ieee80211_output.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. /* $OpenBSD: ieee80211_output.c,v 1.97 2015/07/15 22:16:42 deraadt Exp $ */
  2. /* $NetBSD: ieee80211_output.c,v 1.13 2004/05/31 11:02:55 dyoung Exp $ */
  3. /*-
  4. * Copyright (c) 2001 Atsushi Onoe
  5. * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
  6. * Copyright (c) 2007-2009 Damien Bergamini
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "bpfilter.h"
  32. #include "vlan.h"
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/mbuf.h>
  36. #include <sys/kernel.h>
  37. #include <sys/socket.h>
  38. #include <sys/sockio.h>
  39. #include <sys/endian.h>
  40. #include <sys/errno.h>
  41. #include <sys/sysctl.h>
  42. #include <net/if.h>
  43. #include <net/if_dl.h>
  44. #include <net/if_media.h>
  45. #include <net/if_llc.h>
  46. #include <net/bpf.h>
  47. #include <netinet/in.h>
  48. #include <netinet/if_ether.h>
  49. #include <netinet/ip.h>
  50. #ifdef INET6
  51. #include <netinet/ip6.h>
  52. #endif
  53. #if NVLAN > 0
  54. #include <net/if_types.h>
  55. #include <net/if_vlan_var.h>
  56. #endif
  57. #include <net80211/ieee80211_var.h>
  58. #include <net80211/ieee80211_priv.h>
  59. int ieee80211_classify(struct ieee80211com *, struct mbuf *);
  60. int ieee80211_mgmt_output(struct ifnet *, struct ieee80211_node *,
  61. struct mbuf *, int);
  62. u_int8_t *ieee80211_add_rsn_body(u_int8_t *, struct ieee80211com *,
  63. const struct ieee80211_node *, int);
  64. struct mbuf *ieee80211_getmgmt(int, int, u_int);
  65. struct mbuf *ieee80211_get_probe_req(struct ieee80211com *,
  66. struct ieee80211_node *);
  67. #ifndef IEEE80211_STA_ONLY
  68. struct mbuf *ieee80211_get_probe_resp(struct ieee80211com *,
  69. struct ieee80211_node *);
  70. #endif
  71. struct mbuf *ieee80211_get_auth(struct ieee80211com *,
  72. struct ieee80211_node *, u_int16_t, u_int16_t);
  73. struct mbuf *ieee80211_get_deauth(struct ieee80211com *,
  74. struct ieee80211_node *, u_int16_t);
  75. struct mbuf *ieee80211_get_assoc_req(struct ieee80211com *,
  76. struct ieee80211_node *, int);
  77. #ifndef IEEE80211_STA_ONLY
  78. struct mbuf *ieee80211_get_assoc_resp(struct ieee80211com *,
  79. struct ieee80211_node *, u_int16_t);
  80. #endif
  81. struct mbuf *ieee80211_get_disassoc(struct ieee80211com *,
  82. struct ieee80211_node *, u_int16_t);
  83. #ifndef IEEE80211_NO_HT
  84. struct mbuf *ieee80211_get_addba_req(struct ieee80211com *,
  85. struct ieee80211_node *, u_int8_t);
  86. struct mbuf *ieee80211_get_addba_resp(struct ieee80211com *,
  87. struct ieee80211_node *, u_int8_t, u_int8_t, u_int16_t);
  88. struct mbuf *ieee80211_get_delba(struct ieee80211com *,
  89. struct ieee80211_node *, u_int8_t, u_int8_t, u_int16_t);
  90. #endif
  91. struct mbuf *ieee80211_get_sa_query(struct ieee80211com *,
  92. struct ieee80211_node *, u_int8_t);
  93. struct mbuf *ieee80211_get_action(struct ieee80211com *,
  94. struct ieee80211_node *, u_int8_t, u_int8_t, int);
  95. /*
  96. * IEEE 802.11 output routine. Normally this will directly call the
  97. * Ethernet output routine because 802.11 encapsulation is called
  98. * later by the driver. This function can be used to send raw frames
  99. * if the mbuf has been tagged with a 802.11 data link type.
  100. */
  101. int
  102. ieee80211_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
  103. struct rtentry *rt)
  104. {
  105. struct ieee80211_frame *wh;
  106. struct m_tag *mtag;
  107. int error = 0;
  108. /* Interface has to be up and running */
  109. if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
  110. (IFF_UP | IFF_RUNNING)) {
  111. error = ENETDOWN;
  112. goto bad;
  113. }
  114. /* Try to get the DLT from a mbuf tag */
  115. if ((mtag = m_tag_find(m, PACKET_TAG_DLT, NULL)) != NULL) {
  116. struct ieee80211com *ic = (void *)ifp;
  117. u_int dlt = *(u_int *)(mtag + 1);
  118. /* Fallback to ethernet for non-802.11 linktypes */
  119. if (!(dlt == DLT_IEEE802_11 || dlt == DLT_IEEE802_11_RADIO))
  120. goto fallback;
  121. if (m->m_pkthdr.len < sizeof(struct ieee80211_frame_min))
  122. return (EINVAL);
  123. wh = mtod(m, struct ieee80211_frame *);
  124. if ((wh->i_fc[0] & IEEE80211_FC0_VERSION_MASK) !=
  125. IEEE80211_FC0_VERSION_0)
  126. return (EINVAL);
  127. if (!(ic->ic_caps & IEEE80211_C_RAWCTL) &&
  128. (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
  129. IEEE80211_FC0_TYPE_CTL)
  130. return (EINVAL);
  131. return (if_enqueue(ifp, m));
  132. }
  133. fallback:
  134. return (ether_output(ifp, m, dst, rt));
  135. bad:
  136. m_freem(m);
  137. return (error);
  138. }
  139. /*
  140. * Send a management frame to the specified node. The node pointer
  141. * must have a reference as the pointer will be passed to the driver
  142. * and potentially held for a long time. If the frame is successfully
  143. * dispatched to the driver, then it is responsible for freeing the
  144. * reference (and potentially free'ing up any associated storage).
  145. */
  146. int
  147. ieee80211_mgmt_output(struct ifnet *ifp, struct ieee80211_node *ni,
  148. struct mbuf *m, int type)
  149. {
  150. struct ieee80211com *ic = (void *)ifp;
  151. struct ieee80211_frame *wh;
  152. if (ni == NULL)
  153. panic("null node");
  154. ni->ni_inact = 0;
  155. /*
  156. * We want to pass the node down to the driver's start
  157. * routine. We could stick this in an m_tag and tack that
  158. * on to the mbuf. However that's rather expensive to do
  159. * for every frame so instead we stuff it in a special pkthdr
  160. * field.
  161. */
  162. M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
  163. if (m == NULL)
  164. return ENOMEM;
  165. m->m_pkthdr.ph_cookie = ni;
  166. wh = mtod(m, struct ieee80211_frame *);
  167. wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | type;
  168. wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
  169. *(u_int16_t *)&wh->i_dur[0] = 0;
  170. *(u_int16_t *)&wh->i_seq[0] =
  171. htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
  172. ni->ni_txseq++;
  173. IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_macaddr);
  174. IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
  175. IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
  176. /* check if protection is required for this mgmt frame */
  177. if ((ic->ic_caps & IEEE80211_C_MFP) &&
  178. (type == IEEE80211_FC0_SUBTYPE_DISASSOC ||
  179. type == IEEE80211_FC0_SUBTYPE_DEAUTH ||
  180. type == IEEE80211_FC0_SUBTYPE_ACTION)) {
  181. /*
  182. * Hack: we should not set the Protected bit in outgoing
  183. * group management frames, however it is used as an
  184. * indication to the drivers that they must encrypt the
  185. * frame. Drivers should clear this bit from group
  186. * management frames (software crypto code will do it).
  187. * XXX could use an mbuf flag..
  188. */
  189. if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
  190. (ni->ni_flags & IEEE80211_NODE_TXMGMTPROT))
  191. wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
  192. }
  193. if (ifp->if_flags & IFF_DEBUG) {
  194. /* avoid to print too many frames */
  195. if (
  196. #ifndef IEEE80211_STA_ONLY
  197. ic->ic_opmode == IEEE80211_M_IBSS ||
  198. #endif
  199. #ifdef IEEE80211_DEBUG
  200. ieee80211_debug > 1 ||
  201. #endif
  202. (type & IEEE80211_FC0_SUBTYPE_MASK) !=
  203. IEEE80211_FC0_SUBTYPE_PROBE_RESP)
  204. printf("%s: sending %s to %s on channel %u mode %s\n",
  205. ifp->if_xname,
  206. ieee80211_mgt_subtype_name[
  207. (type & IEEE80211_FC0_SUBTYPE_MASK)
  208. >> IEEE80211_FC0_SUBTYPE_SHIFT],
  209. ether_sprintf(ni->ni_macaddr),
  210. ieee80211_chan2ieee(ic, ni->ni_chan),
  211. ieee80211_phymode_name[
  212. ieee80211_chan2mode(ic, ni->ni_chan)]);
  213. }
  214. #ifndef IEEE80211_STA_ONLY
  215. if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
  216. ieee80211_pwrsave(ic, m, ni) != 0)
  217. return 0;
  218. #endif
  219. IF_ENQUEUE(&ic->ic_mgtq, m);
  220. ifp->if_timer = 1;
  221. (*ifp->if_start)(ifp);
  222. return 0;
  223. }
  224. /*-
  225. * EDCA tables are computed using the following formulas:
  226. *
  227. * 1) EDCATable (non-AP QSTA)
  228. *
  229. * AC CWmin CWmax AIFSN TXOP limit(ms)
  230. * -------------------------------------------------------------
  231. * AC_BK aCWmin aCWmax 7 0
  232. * AC_BE aCWmin aCWmax 3 0
  233. * AC_VI (aCWmin+1)/2-1 aCWmin 2 agn=3.008 b=6.016 others=0
  234. * AC_VO (aCWmin+1)/4-1 (aCWmin+1)/2-1 2 agn=1.504 b=3.264 others=0
  235. *
  236. * 2) QAPEDCATable (QAP)
  237. *
  238. * AC CWmin CWmax AIFSN TXOP limit(ms)
  239. * -------------------------------------------------------------
  240. * AC_BK aCWmin aCWmax 7 0
  241. * AC_BE aCWmin 4*(aCWmin+1)-1 3 0
  242. * AC_VI (aCWmin+1)/2-1 aCWmin 1 agn=3.008 b=6.016 others=0
  243. * AC_VO (aCWmin+1)/4-1 (aCWmin+1)/2-1 1 agn=1.504 b=3.264 others=0
  244. *
  245. * and the following aCWmin/aCWmax values:
  246. *
  247. * PHY aCWmin aCWmax
  248. * ---------------------------
  249. * 11A 15 1023
  250. * 11B 31 1023
  251. * 11G 15* 1023 (*) aCWmin(1)
  252. * Turbo A/G 7 1023 (Atheros proprietary mode)
  253. */
  254. #if 0
  255. static const struct ieee80211_edca_ac_params
  256. ieee80211_edca_table[IEEE80211_MODE_MAX][EDCA_NUM_AC] = {
  257. [IEEE80211_MODE_11B] = {
  258. [EDCA_AC_BK] = { 5, 10, 7, 0 },
  259. [EDCA_AC_BE] = { 5, 10, 3, 0 },
  260. [EDCA_AC_VI] = { 4, 5, 2, 188 },
  261. [EDCA_AC_VO] = { 3, 4, 2, 102 }
  262. },
  263. [IEEE80211_MODE_11A] = {
  264. [EDCA_AC_BK] = { 4, 10, 7, 0 },
  265. [EDCA_AC_BE] = { 4, 10, 3, 0 },
  266. [EDCA_AC_VI] = { 3, 4, 2, 94 },
  267. [EDCA_AC_VO] = { 2, 3, 2, 47 }
  268. },
  269. [IEEE80211_MODE_11G] = {
  270. [EDCA_AC_BK] = { 4, 10, 7, 0 },
  271. [EDCA_AC_BE] = { 4, 10, 3, 0 },
  272. [EDCA_AC_VI] = { 3, 4, 2, 94 },
  273. [EDCA_AC_VO] = { 2, 3, 2, 47 }
  274. },
  275. [IEEE80211_MODE_TURBO] = {
  276. [EDCA_AC_BK] = { 3, 10, 7, 0 },
  277. [EDCA_AC_BE] = { 3, 10, 2, 0 },
  278. [EDCA_AC_VI] = { 2, 3, 2, 94 },
  279. [EDCA_AC_VO] = { 2, 2, 1, 47 }
  280. }
  281. };
  282. #endif
  283. #ifndef IEEE80211_STA_ONLY
  284. static const struct ieee80211_edca_ac_params
  285. ieee80211_qap_edca_table[IEEE80211_MODE_MAX][EDCA_NUM_AC] = {
  286. [IEEE80211_MODE_11B] = {
  287. [EDCA_AC_BK] = { 5, 10, 7, 0 },
  288. [EDCA_AC_BE] = { 5, 7, 3, 0 },
  289. [EDCA_AC_VI] = { 4, 5, 1, 188 },
  290. [EDCA_AC_VO] = { 3, 4, 1, 102 }
  291. },
  292. [IEEE80211_MODE_11A] = {
  293. [EDCA_AC_BK] = { 4, 10, 7, 0 },
  294. [EDCA_AC_BE] = { 4, 6, 3, 0 },
  295. [EDCA_AC_VI] = { 3, 4, 1, 94 },
  296. [EDCA_AC_VO] = { 2, 3, 1, 47 }
  297. },
  298. [IEEE80211_MODE_11G] = {
  299. [EDCA_AC_BK] = { 4, 10, 7, 0 },
  300. [EDCA_AC_BE] = { 4, 6, 3, 0 },
  301. [EDCA_AC_VI] = { 3, 4, 1, 94 },
  302. [EDCA_AC_VO] = { 2, 3, 1, 47 }
  303. },
  304. [IEEE80211_MODE_TURBO] = {
  305. [EDCA_AC_BK] = { 3, 10, 7, 0 },
  306. [EDCA_AC_BE] = { 3, 5, 2, 0 },
  307. [EDCA_AC_VI] = { 2, 3, 1, 94 },
  308. [EDCA_AC_VO] = { 2, 2, 1, 47 }
  309. }
  310. };
  311. #endif /* IEEE80211_STA_ONLY */
  312. /*
  313. * Return the EDCA Access Category to be used for transmitting a frame with
  314. * user-priority `up'.
  315. */
  316. enum ieee80211_edca_ac
  317. ieee80211_up_to_ac(struct ieee80211com *ic, int up)
  318. {
  319. /* see Table 9-1 */
  320. static const enum ieee80211_edca_ac up_to_ac[] = {
  321. EDCA_AC_BE, /* BE */
  322. EDCA_AC_BK, /* BK */
  323. EDCA_AC_BK, /* -- */
  324. EDCA_AC_BE, /* EE */
  325. EDCA_AC_VI, /* CL */
  326. EDCA_AC_VI, /* VI */
  327. EDCA_AC_VO, /* VO */
  328. EDCA_AC_VO /* NC */
  329. };
  330. enum ieee80211_edca_ac ac;
  331. ac = (up <= 7) ? up_to_ac[up] : EDCA_AC_BE;
  332. #ifndef IEEE80211_STA_ONLY
  333. if (ic->ic_opmode == IEEE80211_M_HOSTAP)
  334. return ac;
  335. #endif
  336. /*
  337. * We do not support the admission control procedure defined in
  338. * IEEE Std 802.11-2007 section 9.9.3.1.2. The spec says that
  339. * non-AP QSTAs that don't support this procedure shall use EDCA
  340. * parameters of a lower priority AC that does not require
  341. * admission control.
  342. */
  343. while (ac != EDCA_AC_BK && ic->ic_edca_ac[ac].ac_acm) {
  344. switch (ac) {
  345. case EDCA_AC_BK:
  346. /* can't get there */
  347. break;
  348. case EDCA_AC_BE:
  349. /* BE shouldn't require admission control */
  350. ac = EDCA_AC_BK;
  351. break;
  352. case EDCA_AC_VI:
  353. ac = EDCA_AC_BE;
  354. break;
  355. case EDCA_AC_VO:
  356. ac = EDCA_AC_VI;
  357. break;
  358. }
  359. }
  360. return ac;
  361. }
  362. /*
  363. * Get mbuf's user-priority: if mbuf is not VLAN tagged, select user-priority
  364. * based on the DSCP (Differentiated Services Codepoint) field.
  365. */
  366. int
  367. ieee80211_classify(struct ieee80211com *ic, struct mbuf *m)
  368. {
  369. struct ether_header *eh;
  370. u_int8_t ds_field;
  371. #if NVLAN > 0
  372. if (m->m_flags & M_VLANTAG) /* use VLAN 802.1D user-priority */
  373. return EVL_PRIOFTAG(m->m_pkthdr.ether_vtag);
  374. #endif
  375. eh = mtod(m, struct ether_header *);
  376. if (eh->ether_type == htons(ETHERTYPE_IP)) {
  377. struct ip *ip = (struct ip *)&eh[1];
  378. if (ip->ip_v != 4)
  379. return 0;
  380. ds_field = ip->ip_tos;
  381. }
  382. #ifdef INET6
  383. else if (eh->ether_type == htons(ETHERTYPE_IPV6)) {
  384. struct ip6_hdr *ip6 = (struct ip6_hdr *)&eh[1];
  385. u_int32_t flowlabel;
  386. flowlabel = ntohl(ip6->ip6_flow);
  387. if ((flowlabel >> 28) != 6)
  388. return 0;
  389. ds_field = (flowlabel >> 20) & 0xff;
  390. }
  391. #endif /* INET6 */
  392. else /* neither IPv4 nor IPv6 */
  393. return 0;
  394. /*
  395. * Map Differentiated Services Codepoint field (see RFC2474).
  396. * Preserves backward compatibility with IP Precedence field.
  397. */
  398. switch (ds_field & 0xfc) {
  399. case IPTOS_PREC_PRIORITY:
  400. return 2;
  401. case IPTOS_PREC_IMMEDIATE:
  402. return 1;
  403. case IPTOS_PREC_FLASH:
  404. return 3;
  405. case IPTOS_PREC_FLASHOVERRIDE:
  406. return 4;
  407. case IPTOS_PREC_CRITIC_ECP:
  408. return 5;
  409. case IPTOS_PREC_INTERNETCONTROL:
  410. return 6;
  411. case IPTOS_PREC_NETCONTROL:
  412. return 7;
  413. }
  414. return 0; /* default to Best-Effort */
  415. }
  416. /*
  417. * Encapsulate an outbound data frame. The mbuf chain is updated and
  418. * a reference to the destination node is returned. If an error is
  419. * encountered NULL is returned and the node reference will also be NULL.
  420. *
  421. * NB: The caller is responsible for free'ing a returned node reference.
  422. * The convention is ic_bss is not reference counted; the caller must
  423. * maintain that.
  424. */
  425. struct mbuf *
  426. ieee80211_encap(struct ifnet *ifp, struct mbuf *m, struct ieee80211_node **pni)
  427. {
  428. struct ieee80211com *ic = (void *)ifp;
  429. struct ether_header eh;
  430. struct ieee80211_frame *wh;
  431. struct ieee80211_node *ni = NULL;
  432. struct llc *llc;
  433. struct m_tag *mtag;
  434. u_int8_t *addr;
  435. u_int dlt, hdrlen;
  436. int addqos, tid;
  437. /* Handle raw frames if mbuf is tagged as 802.11 */
  438. if ((mtag = m_tag_find(m, PACKET_TAG_DLT, NULL)) != NULL) {
  439. dlt = *(u_int *)(mtag + 1);
  440. if (!(dlt == DLT_IEEE802_11 || dlt == DLT_IEEE802_11_RADIO))
  441. goto fallback;
  442. wh = mtod(m, struct ieee80211_frame *);
  443. switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
  444. case IEEE80211_FC1_DIR_NODS:
  445. case IEEE80211_FC1_DIR_FROMDS:
  446. addr = wh->i_addr1;
  447. break;
  448. case IEEE80211_FC1_DIR_DSTODS:
  449. case IEEE80211_FC1_DIR_TODS:
  450. addr = wh->i_addr3;
  451. break;
  452. default:
  453. goto bad;
  454. }
  455. ni = ieee80211_find_txnode(ic, addr);
  456. if (ni == NULL)
  457. ni = ieee80211_ref_node(ic->ic_bss);
  458. if (ni == NULL) {
  459. printf("%s: no node for dst %s, "
  460. "discard raw tx frame\n", ifp->if_xname,
  461. ether_sprintf(addr));
  462. ic->ic_stats.is_tx_nonode++;
  463. goto bad;
  464. }
  465. ni->ni_inact = 0;
  466. *pni = ni;
  467. return (m);
  468. }
  469. fallback:
  470. if (m->m_len < sizeof(struct ether_header)) {
  471. m = m_pullup(m, sizeof(struct ether_header));
  472. if (m == NULL) {
  473. ic->ic_stats.is_tx_nombuf++;
  474. goto bad;
  475. }
  476. }
  477. memcpy(&eh, mtod(m, caddr_t), sizeof(struct ether_header));
  478. ni = ieee80211_find_txnode(ic, eh.ether_dhost);
  479. if (ni == NULL) {
  480. DPRINTF(("no node for dst %s, discard frame\n",
  481. ether_sprintf(eh.ether_dhost)));
  482. ic->ic_stats.is_tx_nonode++;
  483. goto bad;
  484. }
  485. if ((ic->ic_flags & IEEE80211_F_RSNON) &&
  486. !ni->ni_port_valid &&
  487. eh.ether_type != htons(ETHERTYPE_PAE)) {
  488. DPRINTF(("port not valid: %s\n",
  489. ether_sprintf(eh.ether_dhost)));
  490. ic->ic_stats.is_tx_noauth++;
  491. goto bad;
  492. }
  493. if ((ic->ic_flags & IEEE80211_F_COUNTERM) &&
  494. ni->ni_rsncipher == IEEE80211_CIPHER_TKIP)
  495. /* XXX TKIP countermeasures! */;
  496. ni->ni_inact = 0;
  497. if ((ic->ic_flags & IEEE80211_F_QOS) &&
  498. (ni->ni_flags & IEEE80211_NODE_QOS) &&
  499. /* do not QoS-encapsulate EAPOL frames */
  500. eh.ether_type != htons(ETHERTYPE_PAE)) {
  501. tid = ieee80211_classify(ic, m);
  502. hdrlen = sizeof(struct ieee80211_qosframe);
  503. addqos = 1;
  504. } else {
  505. hdrlen = sizeof(struct ieee80211_frame);
  506. addqos = 0;
  507. }
  508. m_adj(m, sizeof(struct ether_header) - LLC_SNAPFRAMELEN);
  509. llc = mtod(m, struct llc *);
  510. llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
  511. llc->llc_control = LLC_UI;
  512. llc->llc_snap.org_code[0] = 0;
  513. llc->llc_snap.org_code[1] = 0;
  514. llc->llc_snap.org_code[2] = 0;
  515. llc->llc_snap.ether_type = eh.ether_type;
  516. M_PREPEND(m, hdrlen, M_DONTWAIT);
  517. if (m == NULL) {
  518. ic->ic_stats.is_tx_nombuf++;
  519. goto bad;
  520. }
  521. wh = mtod(m, struct ieee80211_frame *);
  522. wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_DATA;
  523. *(u_int16_t *)&wh->i_dur[0] = 0;
  524. if (addqos) {
  525. struct ieee80211_qosframe *qwh =
  526. (struct ieee80211_qosframe *)wh;
  527. u_int16_t qos = tid;
  528. if (ic->ic_tid_noack & (1 << tid))
  529. qos |= IEEE80211_QOS_ACK_POLICY_NOACK;
  530. #ifndef IEEE80211_NO_HT
  531. else if (ni->ni_tx_ba[tid].ba_state == IEEE80211_BA_AGREED)
  532. qos |= IEEE80211_QOS_ACK_POLICY_BA;
  533. #endif
  534. qwh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_QOS;
  535. *(u_int16_t *)qwh->i_qos = htole16(qos);
  536. *(u_int16_t *)qwh->i_seq =
  537. htole16(ni->ni_qos_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT);
  538. ni->ni_qos_txseqs[tid]++;
  539. } else {
  540. *(u_int16_t *)&wh->i_seq[0] =
  541. htole16(ni->ni_txseq << IEEE80211_SEQ_SEQ_SHIFT);
  542. ni->ni_txseq++;
  543. }
  544. switch (ic->ic_opmode) {
  545. case IEEE80211_M_STA:
  546. wh->i_fc[1] = IEEE80211_FC1_DIR_TODS;
  547. IEEE80211_ADDR_COPY(wh->i_addr1, ni->ni_bssid);
  548. IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
  549. IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_dhost);
  550. break;
  551. #ifndef IEEE80211_STA_ONLY
  552. case IEEE80211_M_IBSS:
  553. case IEEE80211_M_AHDEMO:
  554. wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
  555. IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
  556. IEEE80211_ADDR_COPY(wh->i_addr2, eh.ether_shost);
  557. IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_bss->ni_bssid);
  558. break;
  559. case IEEE80211_M_HOSTAP:
  560. wh->i_fc[1] = IEEE80211_FC1_DIR_FROMDS;
  561. IEEE80211_ADDR_COPY(wh->i_addr1, eh.ether_dhost);
  562. IEEE80211_ADDR_COPY(wh->i_addr2, ni->ni_bssid);
  563. IEEE80211_ADDR_COPY(wh->i_addr3, eh.ether_shost);
  564. break;
  565. #endif
  566. default:
  567. /* should not get there */
  568. goto bad;
  569. }
  570. if ((ic->ic_flags & IEEE80211_F_WEPON) ||
  571. ((ic->ic_flags & IEEE80211_F_RSNON) &&
  572. (ni->ni_flags & IEEE80211_NODE_TXPROT)))
  573. wh->i_fc[1] |= IEEE80211_FC1_PROTECTED;
  574. #ifndef IEEE80211_STA_ONLY
  575. if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
  576. ieee80211_pwrsave(ic, m, ni) != 0) {
  577. *pni = NULL;
  578. return NULL;
  579. }
  580. #endif
  581. *pni = ni;
  582. return m;
  583. bad:
  584. m_freem(m);
  585. if (ni != NULL)
  586. ieee80211_release_node(ic, ni);
  587. *pni = NULL;
  588. return NULL;
  589. }
  590. /*
  591. * Add a Capability Information field to a frame (see 7.3.1.4).
  592. */
  593. u_int8_t *
  594. ieee80211_add_capinfo(u_int8_t *frm, struct ieee80211com *ic,
  595. const struct ieee80211_node *ni)
  596. {
  597. u_int16_t capinfo;
  598. #ifndef IEEE80211_STA_ONLY
  599. if (ic->ic_opmode == IEEE80211_M_IBSS)
  600. capinfo = IEEE80211_CAPINFO_IBSS;
  601. else if (ic->ic_opmode == IEEE80211_M_HOSTAP)
  602. capinfo = IEEE80211_CAPINFO_ESS;
  603. else
  604. #endif
  605. capinfo = 0;
  606. #ifndef IEEE80211_STA_ONLY
  607. if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
  608. (ic->ic_flags & (IEEE80211_F_WEPON | IEEE80211_F_RSNON)))
  609. capinfo |= IEEE80211_CAPINFO_PRIVACY;
  610. #endif
  611. /* NB: some 11a AP's reject the request when short preamble is set */
  612. if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
  613. IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
  614. capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
  615. if (ic->ic_flags & IEEE80211_F_SHSLOT)
  616. capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
  617. LE_WRITE_2(frm, capinfo);
  618. return frm + 2;
  619. }
  620. /*
  621. * Add an SSID element to a frame (see 7.3.2.1).
  622. */
  623. u_int8_t *
  624. ieee80211_add_ssid(u_int8_t *frm, const u_int8_t *ssid, u_int len)
  625. {
  626. *frm++ = IEEE80211_ELEMID_SSID;
  627. *frm++ = len;
  628. memcpy(frm, ssid, len);
  629. return frm + len;
  630. }
  631. /*
  632. * Add a supported rates element to a frame (see 7.3.2.2).
  633. */
  634. u_int8_t *
  635. ieee80211_add_rates(u_int8_t *frm, const struct ieee80211_rateset *rs)
  636. {
  637. int nrates;
  638. *frm++ = IEEE80211_ELEMID_RATES;
  639. nrates = min(rs->rs_nrates, IEEE80211_RATE_SIZE);
  640. *frm++ = nrates;
  641. memcpy(frm, rs->rs_rates, nrates);
  642. return frm + nrates;
  643. }
  644. #ifndef IEEE80211_STA_ONLY
  645. /*
  646. * Add a DS Parameter Set element to a frame (see 7.3.2.4).
  647. */
  648. u_int8_t *
  649. ieee80211_add_ds_params(u_int8_t *frm, struct ieee80211com *ic,
  650. const struct ieee80211_node *ni)
  651. {
  652. *frm++ = IEEE80211_ELEMID_DSPARMS;
  653. *frm++ = 1;
  654. *frm++ = ieee80211_chan2ieee(ic, ni->ni_chan);
  655. return frm;
  656. }
  657. /*
  658. * Add a TIM element to a frame (see 7.3.2.6 and Annex L).
  659. */
  660. u_int8_t *
  661. ieee80211_add_tim(u_int8_t *frm, struct ieee80211com *ic)
  662. {
  663. u_int i, offset = 0, len;
  664. /* find first non-zero octet in the virtual bit map */
  665. for (i = 0; i < ic->ic_tim_len && ic->ic_tim_bitmap[i] == 0; i++);
  666. /* clear the lsb as it is reserved for the broadcast indication bit */
  667. if (i < ic->ic_tim_len)
  668. offset = i & ~1;
  669. /* find last non-zero octet in the virtual bit map */
  670. for (i = ic->ic_tim_len - 1; i > 0 && ic->ic_tim_bitmap[i] == 0; i--);
  671. len = i - offset + 1;
  672. *frm++ = IEEE80211_ELEMID_TIM;
  673. *frm++ = len + 3; /* length */
  674. *frm++ = ic->ic_dtim_count; /* DTIM count */
  675. *frm++ = ic->ic_dtim_period; /* DTIM period */
  676. /* Bitmap Control */
  677. *frm = offset;
  678. /* set broadcast/multicast indication bit if necessary */
  679. if (ic->ic_dtim_count == 0 && ic->ic_tim_mcast_pending)
  680. *frm |= 0x01;
  681. frm++;
  682. /* Partial Virtual Bitmap */
  683. memcpy(frm, &ic->ic_tim_bitmap[offset], len);
  684. return frm + len;
  685. }
  686. /*
  687. * Add an IBSS Parameter Set element to a frame (see 7.3.2.7).
  688. */
  689. u_int8_t *
  690. ieee80211_add_ibss_params(u_int8_t *frm, const struct ieee80211_node *ni)
  691. {
  692. *frm++ = IEEE80211_ELEMID_IBSSPARMS;
  693. *frm++ = 2;
  694. LE_WRITE_2(frm, 0); /* TODO: ATIM window */
  695. return frm + 2;
  696. }
  697. /*
  698. * Add an EDCA Parameter Set element to a frame (see 7.3.2.29).
  699. */
  700. u_int8_t *
  701. ieee80211_add_edca_params(u_int8_t *frm, struct ieee80211com *ic)
  702. {
  703. const struct ieee80211_edca_ac_params *edca;
  704. int aci;
  705. *frm++ = IEEE80211_ELEMID_EDCAPARMS;
  706. *frm++ = 18; /* length */
  707. *frm++ = 0; /* QoS Info */
  708. *frm++ = 0; /* reserved */
  709. /* setup AC Parameter Records */
  710. edca = ieee80211_qap_edca_table[ic->ic_curmode];
  711. for (aci = 0; aci < EDCA_NUM_AC; aci++) {
  712. const struct ieee80211_edca_ac_params *ac = &edca[aci];
  713. *frm++ = (aci << 5) | ((ac->ac_acm & 0x1) << 4) |
  714. (ac->ac_aifsn & 0xf);
  715. *frm++ = (ac->ac_ecwmax << 4) |
  716. (ac->ac_ecwmin & 0xf);
  717. LE_WRITE_2(frm, ac->ac_txoplimit); frm += 2;
  718. }
  719. return frm;
  720. }
  721. /*
  722. * Add an ERP element to a frame (see 7.3.2.13).
  723. */
  724. u_int8_t *
  725. ieee80211_add_erp(u_int8_t *frm, struct ieee80211com *ic)
  726. {
  727. u_int8_t erp;
  728. *frm++ = IEEE80211_ELEMID_ERP;
  729. *frm++ = 1;
  730. erp = 0;
  731. /*
  732. * The NonERP_Present bit shall be set to 1 when a NonERP STA
  733. * is associated with the BSS.
  734. */
  735. if (ic->ic_nonerpsta != 0)
  736. erp |= IEEE80211_ERP_NON_ERP_PRESENT;
  737. /*
  738. * If one or more NonERP STAs are associated in the BSS, the
  739. * Use_Protection bit shall be set to 1 in transmitted ERP
  740. * Information Elements.
  741. */
  742. if (ic->ic_flags & IEEE80211_F_USEPROT)
  743. erp |= IEEE80211_ERP_USE_PROTECTION;
  744. /*
  745. * The Barker_Preamble_Mode bit shall be set to 1 by the ERP
  746. * Information Element sender if one or more associated NonERP
  747. * STAs are not short preamble capable.
  748. */
  749. if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE))
  750. erp |= IEEE80211_ERP_BARKER_MODE;
  751. *frm++ = erp;
  752. return frm;
  753. }
  754. #endif /* IEEE80211_STA_ONLY */
  755. /*
  756. * Add a QoS Capability element to a frame (see 7.3.2.35).
  757. */
  758. u_int8_t *
  759. ieee80211_add_qos_capability(u_int8_t *frm, struct ieee80211com *ic)
  760. {
  761. *frm++ = IEEE80211_ELEMID_QOS_CAP;
  762. *frm++ = 1;
  763. *frm++ = 0; /* QoS Info */
  764. return frm;
  765. }
  766. /*
  767. * Add an RSN element to a frame (see 7.3.2.25).
  768. */
  769. u_int8_t *
  770. ieee80211_add_rsn_body(u_int8_t *frm, struct ieee80211com *ic,
  771. const struct ieee80211_node *ni, int wpa)
  772. {
  773. const u_int8_t *oui = wpa ? MICROSOFT_OUI : IEEE80211_OUI;
  774. u_int8_t *pcount;
  775. u_int16_t count;
  776. /* write Version field */
  777. LE_WRITE_2(frm, 1); frm += 2;
  778. /* write Group Data Cipher Suite field (see Table 20da) */
  779. memcpy(frm, oui, 3); frm += 3;
  780. switch (ni->ni_rsngroupcipher) {
  781. case IEEE80211_CIPHER_WEP40:
  782. *frm++ = 1;
  783. break;
  784. case IEEE80211_CIPHER_TKIP:
  785. *frm++ = 2;
  786. break;
  787. case IEEE80211_CIPHER_CCMP:
  788. *frm++ = 4;
  789. break;
  790. case IEEE80211_CIPHER_WEP104:
  791. *frm++ = 5;
  792. break;
  793. default:
  794. /* can't get there */
  795. panic("invalid group data cipher!");
  796. }
  797. pcount = frm; frm += 2;
  798. count = 0;
  799. /* write Pairwise Cipher Suite List */
  800. if (ni->ni_rsnciphers & IEEE80211_CIPHER_USEGROUP) {
  801. memcpy(frm, oui, 3); frm += 3;
  802. *frm++ = 0;
  803. count++;
  804. }
  805. if (ni->ni_rsnciphers & IEEE80211_CIPHER_TKIP) {
  806. memcpy(frm, oui, 3); frm += 3;
  807. *frm++ = 2;
  808. count++;
  809. }
  810. if (ni->ni_rsnciphers & IEEE80211_CIPHER_CCMP) {
  811. memcpy(frm, oui, 3); frm += 3;
  812. *frm++ = 4;
  813. count++;
  814. }
  815. /* write Pairwise Cipher Suite Count field */
  816. LE_WRITE_2(pcount, count);
  817. pcount = frm; frm += 2;
  818. count = 0;
  819. /* write AKM Suite List (see Table 20dc) */
  820. if (ni->ni_rsnakms & IEEE80211_AKM_8021X) {
  821. memcpy(frm, oui, 3); frm += 3;
  822. *frm++ = 1;
  823. count++;
  824. }
  825. if (ni->ni_rsnakms & IEEE80211_AKM_PSK) {
  826. memcpy(frm, oui, 3); frm += 3;
  827. *frm++ = 2;
  828. count++;
  829. }
  830. if (!wpa && (ni->ni_rsnakms & IEEE80211_AKM_SHA256_8021X)) {
  831. memcpy(frm, oui, 3); frm += 3;
  832. *frm++ = 5;
  833. count++;
  834. }
  835. if (!wpa && (ni->ni_rsnakms & IEEE80211_AKM_SHA256_PSK)) {
  836. memcpy(frm, oui, 3); frm += 3;
  837. *frm++ = 6;
  838. count++;
  839. }
  840. /* write AKM Suite List Count field */
  841. LE_WRITE_2(pcount, count);
  842. if (wpa)
  843. return frm;
  844. /* write RSN Capabilities field */
  845. LE_WRITE_2(frm, ni->ni_rsncaps); frm += 2;
  846. if (ni->ni_flags & IEEE80211_NODE_PMKID) {
  847. /* write PMKID Count field */
  848. LE_WRITE_2(frm, 1); frm += 2;
  849. /* write PMKID List (only 1) */
  850. memcpy(frm, ni->ni_pmkid, IEEE80211_PMKID_LEN);
  851. frm += IEEE80211_PMKID_LEN;
  852. } else {
  853. /* no PMKID (PMKID Count=0) */
  854. LE_WRITE_2(frm, 0); frm += 2;
  855. }
  856. if (!(ic->ic_caps & IEEE80211_C_MFP))
  857. return frm;
  858. /* write Group Integrity Cipher Suite field */
  859. memcpy(frm, oui, 3); frm += 3;
  860. switch (ic->ic_rsngroupmgmtcipher) {
  861. case IEEE80211_CIPHER_BIP:
  862. *frm++ = 6;
  863. break;
  864. default:
  865. /* can't get there */
  866. panic("invalid integrity group cipher!");
  867. }
  868. return frm;
  869. }
  870. u_int8_t *
  871. ieee80211_add_rsn(u_int8_t *frm, struct ieee80211com *ic,
  872. const struct ieee80211_node *ni)
  873. {
  874. u_int8_t *plen;
  875. *frm++ = IEEE80211_ELEMID_RSN;
  876. plen = frm++; /* length filled in later */
  877. frm = ieee80211_add_rsn_body(frm, ic, ni, 0);
  878. /* write length field */
  879. *plen = frm - plen - 1;
  880. return frm;
  881. }
  882. /*
  883. * Add a vendor-specific WPA element to a frame.
  884. * This is required for compatibility with Wi-Fi Alliance WPA.
  885. */
  886. u_int8_t *
  887. ieee80211_add_wpa(u_int8_t *frm, struct ieee80211com *ic,
  888. const struct ieee80211_node *ni)
  889. {
  890. u_int8_t *plen;
  891. *frm++ = IEEE80211_ELEMID_VENDOR;
  892. plen = frm++; /* length filled in later */
  893. memcpy(frm, MICROSOFT_OUI, 3); frm += 3;
  894. *frm++ = 1; /* WPA */
  895. frm = ieee80211_add_rsn_body(frm, ic, ni, 1);
  896. /* write length field */
  897. *plen = frm - plen - 1;
  898. return frm;
  899. }
  900. /*
  901. * Add an extended supported rates element to a frame (see 7.3.2.14).
  902. */
  903. u_int8_t *
  904. ieee80211_add_xrates(u_int8_t *frm, const struct ieee80211_rateset *rs)
  905. {
  906. int nrates;
  907. KASSERT(rs->rs_nrates > IEEE80211_RATE_SIZE);
  908. *frm++ = IEEE80211_ELEMID_XRATES;
  909. nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
  910. *frm++ = nrates;
  911. memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
  912. return frm + nrates;
  913. }
  914. #ifndef IEEE80211_NO_HT
  915. /*
  916. * Add an HT Capabilities element to a frame (see 7.3.2.57).
  917. */
  918. u_int8_t *
  919. ieee80211_add_htcaps(u_int8_t *frm, struct ieee80211com *ic)
  920. {
  921. *frm++ = IEEE80211_ELEMID_HTCAPS;
  922. *frm++ = 26;
  923. LE_WRITE_2(frm, ic->ic_htcaps); frm += 2;
  924. *frm++ = 0;
  925. memcpy(frm, ic->ic_sup_mcs, 16); frm += 16;
  926. LE_WRITE_2(frm, ic->ic_htxcaps); frm += 2;
  927. LE_WRITE_4(frm, ic->ic_txbfcaps); frm += 4;
  928. *frm++ = ic->ic_aselcaps;
  929. return frm;
  930. }
  931. #ifndef IEEE80211_STA_ONLY
  932. /*
  933. * Add an HT Operation element to a frame (see 7.3.2.58).
  934. */
  935. u_int8_t *
  936. ieee80211_add_htop(u_int8_t *frm, struct ieee80211com *ic)
  937. {
  938. *frm++ = IEEE80211_ELEMID_HTOP;
  939. *frm++ = 22;
  940. *frm++ = ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan);
  941. LE_WRITE_2(frm, 0); frm += 2;
  942. LE_WRITE_2(frm, 0); frm += 2;
  943. memset(frm, 0, 16); frm += 16;
  944. return frm;
  945. }
  946. #endif /* !IEEE80211_STA_ONLY */
  947. #endif /* !IEEE80211_NO_HT */
  948. #ifndef IEEE80211_STA_ONLY
  949. /*
  950. * Add a Timeout Interval element to a frame (see 7.3.2.49).
  951. */
  952. u_int8_t *
  953. ieee80211_add_tie(u_int8_t *frm, u_int8_t type, u_int32_t value)
  954. {
  955. *frm++ = IEEE80211_ELEMID_TIE;
  956. *frm++ = 5; /* length */
  957. *frm++ = type; /* Timeout Interval type */
  958. LE_WRITE_4(frm, value);
  959. return frm + 4;
  960. }
  961. #endif
  962. struct mbuf *
  963. ieee80211_getmgmt(int flags, int type, u_int pktlen)
  964. {
  965. struct mbuf *m;
  966. /* reserve space for 802.11 header */
  967. pktlen += sizeof(struct ieee80211_frame);
  968. if (pktlen > MCLBYTES)
  969. panic("management frame too large: %u", pktlen);
  970. MGETHDR(m, flags, type);
  971. if (m == NULL)
  972. return NULL;
  973. if (pktlen > MHLEN) {
  974. MCLGET(m, flags);
  975. if (!(m->m_flags & M_EXT))
  976. return m_free(m);
  977. }
  978. m->m_data += sizeof(struct ieee80211_frame);
  979. return m;
  980. }
  981. /*-
  982. * Probe request frame format:
  983. * [tlv] SSID
  984. * [tlv] Supported rates
  985. * [tlv] Extended Supported Rates (802.11g)
  986. * [tlv] HT Capabilities (802.11n)
  987. */
  988. struct mbuf *
  989. ieee80211_get_probe_req(struct ieee80211com *ic, struct ieee80211_node *ni)
  990. {
  991. const struct ieee80211_rateset *rs =
  992. &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
  993. struct mbuf *m;
  994. u_int8_t *frm;
  995. m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA,
  996. 2 + ic->ic_des_esslen +
  997. 2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) +
  998. ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
  999. 2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) +
  1000. ((ni->ni_flags & IEEE80211_NODE_HT) ? 28 : 0));
  1001. if (m == NULL)
  1002. return NULL;
  1003. frm = mtod(m, u_int8_t *);
  1004. frm = ieee80211_add_ssid(frm, ic->ic_des_essid, ic->ic_des_esslen);
  1005. frm = ieee80211_add_rates(frm, rs);
  1006. if (rs->rs_nrates > IEEE80211_RATE_SIZE)
  1007. frm = ieee80211_add_xrates(frm, rs);
  1008. #ifndef IEEE80211_NO_HT
  1009. if (ni->ni_flags & IEEE80211_NODE_HT)
  1010. frm = ieee80211_add_htcaps(frm, ic);
  1011. #endif
  1012. m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
  1013. return m;
  1014. }
  1015. #ifndef IEEE80211_STA_ONLY
  1016. /*-
  1017. * Probe response frame format:
  1018. * [8] Timestamp
  1019. * [2] Beacon interval
  1020. * [2] Capability
  1021. * [tlv] Service Set Identifier (SSID)
  1022. * [tlv] Supported rates
  1023. * [tlv] DS Parameter Set (802.11g)
  1024. * [tlv] ERP Information (802.11g)
  1025. * [tlv] Extended Supported Rates (802.11g)
  1026. * [tlv] RSN (802.11i)
  1027. * [tlv] EDCA Parameter Set (802.11e)
  1028. * [tlv] HT Capabilities (802.11n)
  1029. * [tlv] HT Operation (802.11n)
  1030. */
  1031. struct mbuf *
  1032. ieee80211_get_probe_resp(struct ieee80211com *ic, struct ieee80211_node *ni)
  1033. {
  1034. const struct ieee80211_rateset *rs = &ic->ic_bss->ni_rates;
  1035. struct mbuf *m;
  1036. u_int8_t *frm;
  1037. m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA,
  1038. 8 + 2 + 2 +
  1039. 2 + ni->ni_esslen +
  1040. 2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) +
  1041. 2 + 1 +
  1042. ((ic->ic_opmode == IEEE80211_M_IBSS) ? 2 + 2 : 0) +
  1043. ((ic->ic_curmode == IEEE80211_MODE_11G) ? 2 + 1 : 0) +
  1044. ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
  1045. 2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) +
  1046. (((ic->ic_flags & IEEE80211_F_RSNON) &&
  1047. (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_RSN)) ?
  1048. 2 + IEEE80211_RSNIE_MAXLEN : 0) +
  1049. ((ic->ic_flags & IEEE80211_F_QOS) ? 2 + 18 : 0) +
  1050. (((ic->ic_flags & IEEE80211_F_RSNON) &&
  1051. (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_WPA)) ?
  1052. 2 + IEEE80211_WPAIE_MAXLEN : 0) +
  1053. ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 24 : 0));
  1054. if (m == NULL)
  1055. return NULL;
  1056. frm = mtod(m, u_int8_t *);
  1057. memset(frm, 0, 8); frm += 8; /* timestamp is set by hardware */
  1058. LE_WRITE_2(frm, ic->ic_bss->ni_intval); frm += 2;
  1059. frm = ieee80211_add_capinfo(frm, ic, ni);
  1060. frm = ieee80211_add_ssid(frm, ic->ic_bss->ni_essid,
  1061. ic->ic_bss->ni_esslen);
  1062. frm = ieee80211_add_rates(frm, rs);
  1063. frm = ieee80211_add_ds_params(frm, ic, ni);
  1064. if (ic->ic_opmode == IEEE80211_M_IBSS)
  1065. frm = ieee80211_add_ibss_params(frm, ni);
  1066. if (ic->ic_curmode == IEEE80211_MODE_11G)
  1067. frm = ieee80211_add_erp(frm, ic);
  1068. if (rs->rs_nrates > IEEE80211_RATE_SIZE)
  1069. frm = ieee80211_add_xrates(frm, rs);
  1070. if ((ic->ic_flags & IEEE80211_F_RSNON) &&
  1071. (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_RSN))
  1072. frm = ieee80211_add_rsn(frm, ic, ic->ic_bss);
  1073. if (ic->ic_flags & IEEE80211_F_QOS)
  1074. frm = ieee80211_add_edca_params(frm, ic);
  1075. if ((ic->ic_flags & IEEE80211_F_RSNON) &&
  1076. (ic->ic_bss->ni_rsnprotos & IEEE80211_PROTO_WPA))
  1077. frm = ieee80211_add_wpa(frm, ic, ic->ic_bss);
  1078. #ifndef IEEE80211_NO_HT
  1079. if (ic->ic_flags & IEEE80211_F_HTON) {
  1080. frm = ieee80211_add_htcaps(frm, ic);
  1081. frm = ieee80211_add_htop(frm, ic);
  1082. }
  1083. #endif
  1084. m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
  1085. return m;
  1086. }
  1087. #endif /* IEEE80211_STA_ONLY */
  1088. /*-
  1089. * Authentication frame format:
  1090. * [2] Authentication algorithm number
  1091. * [2] Authentication transaction sequence number
  1092. * [2] Status code
  1093. */
  1094. struct mbuf *
  1095. ieee80211_get_auth(struct ieee80211com *ic, struct ieee80211_node *ni,
  1096. u_int16_t status, u_int16_t seq)
  1097. {
  1098. struct mbuf *m;
  1099. u_int8_t *frm;
  1100. MGETHDR(m, M_DONTWAIT, MT_DATA);
  1101. if (m == NULL)
  1102. return NULL;
  1103. MH_ALIGN(m, 2 * 3);
  1104. m->m_pkthdr.len = m->m_len = 2 * 3;
  1105. frm = mtod(m, u_int8_t *);
  1106. LE_WRITE_2(frm, IEEE80211_AUTH_ALG_OPEN); frm += 2;
  1107. LE_WRITE_2(frm, seq); frm += 2;
  1108. LE_WRITE_2(frm, status);
  1109. return m;
  1110. }
  1111. /*-
  1112. * Deauthentication frame format:
  1113. * [2] Reason code
  1114. */
  1115. struct mbuf *
  1116. ieee80211_get_deauth(struct ieee80211com *ic, struct ieee80211_node *ni,
  1117. u_int16_t reason)
  1118. {
  1119. struct mbuf *m;
  1120. MGETHDR(m, M_DONTWAIT, MT_DATA);
  1121. if (m == NULL)
  1122. return NULL;
  1123. MH_ALIGN(m, 2);
  1124. m->m_pkthdr.len = m->m_len = 2;
  1125. *mtod(m, u_int16_t *) = htole16(reason);
  1126. return m;
  1127. }
  1128. /*-
  1129. * (Re)Association request frame format:
  1130. * [2] Capability information
  1131. * [2] Listen interval
  1132. * [6*] Current AP address (Reassociation only)
  1133. * [tlv] SSID
  1134. * [tlv] Supported rates
  1135. * [tlv] Extended Supported Rates (802.11g)
  1136. * [tlv] RSN (802.11i)
  1137. * [tlv] QoS Capability (802.11e)
  1138. * [tlv] HT Capabilities (802.11n)
  1139. */
  1140. struct mbuf *
  1141. ieee80211_get_assoc_req(struct ieee80211com *ic, struct ieee80211_node *ni,
  1142. int type)
  1143. {
  1144. const struct ieee80211_rateset *rs = &ni->ni_rates;
  1145. struct mbuf *m;
  1146. u_int8_t *frm;
  1147. u_int16_t capinfo;
  1148. m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA,
  1149. 2 + 2 +
  1150. ((type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) ?
  1151. IEEE80211_ADDR_LEN : 0) +
  1152. 2 + ni->ni_esslen +
  1153. 2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) +
  1154. ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
  1155. 2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) +
  1156. (((ic->ic_flags & IEEE80211_F_RSNON) &&
  1157. (ni->ni_rsnprotos & IEEE80211_PROTO_RSN)) ?
  1158. 2 + IEEE80211_RSNIE_MAXLEN : 0) +
  1159. ((ni->ni_flags & IEEE80211_NODE_QOS) ? 2 + 1 : 0) +
  1160. (((ic->ic_flags & IEEE80211_F_RSNON) &&
  1161. (ni->ni_rsnprotos & IEEE80211_PROTO_WPA)) ?
  1162. 2 + IEEE80211_WPAIE_MAXLEN : 0) +
  1163. ((ni->ni_flags & IEEE80211_NODE_HT) ? 28 : 0));
  1164. if (m == NULL)
  1165. return NULL;
  1166. frm = mtod(m, u_int8_t *);
  1167. capinfo = IEEE80211_CAPINFO_ESS;
  1168. if (ic->ic_flags & IEEE80211_F_WEPON)
  1169. capinfo |= IEEE80211_CAPINFO_PRIVACY;
  1170. if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
  1171. IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
  1172. capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
  1173. if (ic->ic_caps & IEEE80211_C_SHSLOT)
  1174. capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
  1175. LE_WRITE_2(frm, capinfo); frm += 2;
  1176. LE_WRITE_2(frm, ic->ic_lintval); frm += 2;
  1177. if (type == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) {
  1178. IEEE80211_ADDR_COPY(frm, ic->ic_bss->ni_bssid);
  1179. frm += IEEE80211_ADDR_LEN;
  1180. }
  1181. frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
  1182. frm = ieee80211_add_rates(frm, rs);
  1183. if (rs->rs_nrates > IEEE80211_RATE_SIZE)
  1184. frm = ieee80211_add_xrates(frm, rs);
  1185. if ((ic->ic_flags & IEEE80211_F_RSNON) &&
  1186. (ni->ni_rsnprotos & IEEE80211_PROTO_RSN))
  1187. frm = ieee80211_add_rsn(frm, ic, ni);
  1188. if (ni->ni_flags & IEEE80211_NODE_QOS)
  1189. frm = ieee80211_add_qos_capability(frm, ic);
  1190. if ((ic->ic_flags & IEEE80211_F_RSNON) &&
  1191. (ni->ni_rsnprotos & IEEE80211_PROTO_WPA))
  1192. frm = ieee80211_add_wpa(frm, ic, ni);
  1193. #ifndef IEEE80211_NO_HT
  1194. if (ni->ni_flags & IEEE80211_NODE_HT)
  1195. frm = ieee80211_add_htcaps(frm, ic);
  1196. #endif
  1197. m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
  1198. return m;
  1199. }
  1200. #ifndef IEEE80211_STA_ONLY
  1201. /*-
  1202. * (Re)Association response frame format:
  1203. * [2] Capability information
  1204. * [2] Status code
  1205. * [2] Association ID (AID)
  1206. * [tlv] Supported rates
  1207. * [tlv] Extended Supported Rates (802.11g)
  1208. * [tlv] EDCA Parameter Set (802.11e)
  1209. * [tlv] Timeout Interval (802.11w)
  1210. * [tlv] HT Capabilities (802.11n)
  1211. * [tlv] HT Operation (802.11n)
  1212. */
  1213. struct mbuf *
  1214. ieee80211_get_assoc_resp(struct ieee80211com *ic, struct ieee80211_node *ni,
  1215. u_int16_t status)
  1216. {
  1217. const struct ieee80211_rateset *rs = &ni->ni_rates;
  1218. struct mbuf *m;
  1219. u_int8_t *frm;
  1220. m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA,
  1221. 2 + 2 + 2 +
  1222. 2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) +
  1223. ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
  1224. 2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) +
  1225. ((ni->ni_flags & IEEE80211_NODE_QOS) ? 2 + 18 : 0) +
  1226. ((status == IEEE80211_STATUS_TRY_AGAIN_LATER) ? 2 + 7 : 0) +
  1227. ((ni->ni_flags & IEEE80211_NODE_HT) ? 28 + 24 : 0));
  1228. if (m == NULL)
  1229. return NULL;
  1230. frm = mtod(m, u_int8_t *);
  1231. frm = ieee80211_add_capinfo(frm, ic, ni);
  1232. LE_WRITE_2(frm, status); frm += 2;
  1233. if (status == IEEE80211_STATUS_SUCCESS)
  1234. LE_WRITE_2(frm, ni->ni_associd);
  1235. else
  1236. LE_WRITE_2(frm, 0);
  1237. frm += 2;
  1238. frm = ieee80211_add_rates(frm, rs);
  1239. if (rs->rs_nrates > IEEE80211_RATE_SIZE)
  1240. frm = ieee80211_add_xrates(frm, rs);
  1241. if (ni->ni_flags & IEEE80211_NODE_QOS)
  1242. frm = ieee80211_add_edca_params(frm, ic);
  1243. if ((ni->ni_flags & IEEE80211_NODE_MFP) &&
  1244. status == IEEE80211_STATUS_TRY_AGAIN_LATER) {
  1245. /* Association Comeback Time */
  1246. frm = ieee80211_add_tie(frm, 3, 1000 /* XXX */);
  1247. }
  1248. #ifndef IEEE80211_NO_HT
  1249. if (ni->ni_flags & IEEE80211_NODE_HT) {
  1250. frm = ieee80211_add_htcaps(frm, ic);
  1251. frm = ieee80211_add_htop(frm, ic);
  1252. }
  1253. #endif
  1254. m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
  1255. return m;
  1256. }
  1257. #endif /* IEEE80211_STA_ONLY */
  1258. /*-
  1259. * Disassociation frame format:
  1260. * [2] Reason code
  1261. */
  1262. struct mbuf *
  1263. ieee80211_get_disassoc(struct ieee80211com *ic, struct ieee80211_node *ni,
  1264. u_int16_t reason)
  1265. {
  1266. struct mbuf *m;
  1267. MGETHDR(m, M_DONTWAIT, MT_DATA);
  1268. if (m == NULL)
  1269. return NULL;
  1270. MH_ALIGN(m, 2);
  1271. m->m_pkthdr.len = m->m_len = 2;
  1272. *mtod(m, u_int16_t *) = htole16(reason);
  1273. return m;
  1274. }
  1275. #ifndef IEEE80211_NO_HT
  1276. /*-
  1277. * ADDBA Request frame format:
  1278. * [1] Category
  1279. * [1] Action
  1280. * [1] Dialog Token
  1281. * [2] Block Ack Parameter Set
  1282. * [2] Block Ack Timeout Value
  1283. * [2] Block Ack Starting Sequence Control
  1284. */
  1285. struct mbuf *
  1286. ieee80211_get_addba_req(struct ieee80211com *ic, struct ieee80211_node *ni,
  1287. u_int8_t tid)
  1288. {
  1289. struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
  1290. struct mbuf *m;
  1291. u_int8_t *frm;
  1292. u_int16_t params;
  1293. m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 9);
  1294. if (m == NULL)
  1295. return m;
  1296. frm = mtod(m, u_int8_t *);
  1297. *frm++ = IEEE80211_CATEG_BA;
  1298. *frm++ = IEEE80211_ACTION_ADDBA_REQ;
  1299. *frm++ = ba->ba_token;
  1300. params = ba->ba_winsize << 6 | tid << 2 | IEEE80211_BA_ACK_POLICY;
  1301. LE_WRITE_2(frm, params); frm += 2;
  1302. LE_WRITE_2(frm, ba->ba_timeout_val); frm += 2;
  1303. LE_WRITE_2(frm, ba->ba_winstart); frm += 2;
  1304. m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
  1305. return m;
  1306. }
  1307. /*-
  1308. * ADDBA Response frame format:
  1309. * [1] Category
  1310. * [1] Action
  1311. * [1] Dialog Token
  1312. * [2] Status Code
  1313. * [2] Block Ack Parameter Set
  1314. * [2] Block Ack Timeout Value
  1315. */
  1316. struct mbuf *
  1317. ieee80211_get_addba_resp(struct ieee80211com *ic, struct ieee80211_node *ni,
  1318. u_int8_t tid, u_int8_t token, u_int16_t status)
  1319. {
  1320. struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid];
  1321. struct mbuf *m;
  1322. u_int8_t *frm;
  1323. u_int16_t params;
  1324. m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 9);
  1325. if (m == NULL)
  1326. return m;
  1327. frm = mtod(m, u_int8_t *);
  1328. *frm++ = IEEE80211_CATEG_BA;
  1329. *frm++ = IEEE80211_ACTION_ADDBA_RESP;
  1330. *frm++ = token;
  1331. LE_WRITE_2(frm, status); frm += 2;
  1332. params = tid << 2 | IEEE80211_BA_ACK_POLICY;
  1333. if (status == 0)
  1334. params |= ba->ba_winsize << 6;
  1335. LE_WRITE_2(frm, params); frm += 2;
  1336. if (status == 0)
  1337. LE_WRITE_2(frm, ba->ba_timeout_val);
  1338. else
  1339. LE_WRITE_2(frm, 0);
  1340. frm += 2;
  1341. m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
  1342. return m;
  1343. }
  1344. /*-
  1345. * DELBA frame format:
  1346. * [1] Category
  1347. * [1] Action
  1348. * [2] DELBA Parameter Set
  1349. * [2] Reason Code
  1350. */
  1351. struct mbuf *
  1352. ieee80211_get_delba(struct ieee80211com *ic, struct ieee80211_node *ni,
  1353. u_int8_t tid, u_int8_t dir, u_int16_t reason)
  1354. {
  1355. struct mbuf *m;
  1356. u_int8_t *frm;
  1357. u_int16_t params;
  1358. m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 6);
  1359. if (m == NULL)
  1360. return m;
  1361. frm = mtod(m, u_int8_t *);
  1362. *frm++ = IEEE80211_CATEG_BA;
  1363. *frm++ = IEEE80211_ACTION_DELBA;
  1364. params = tid << 12;
  1365. if (dir)
  1366. params |= IEEE80211_DELBA_INITIATOR;
  1367. LE_WRITE_2(frm, params); frm += 2;
  1368. LE_WRITE_2(frm, reason); frm += 2;
  1369. m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
  1370. return m;
  1371. }
  1372. #endif /* !IEEE80211_NO_HT */
  1373. /*-
  1374. * SA Query Request/Reponse frame format:
  1375. * [1] Category
  1376. * [1] Action
  1377. * [16] Transaction Identifier
  1378. */
  1379. struct mbuf *
  1380. ieee80211_get_sa_query(struct ieee80211com *ic, struct ieee80211_node *ni,
  1381. u_int8_t action)
  1382. {
  1383. struct mbuf *m;
  1384. u_int8_t *frm;
  1385. m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA, 4);
  1386. if (m == NULL)
  1387. return NULL;
  1388. frm = mtod(m, u_int8_t *);
  1389. *frm++ = IEEE80211_CATEG_SA_QUERY;
  1390. *frm++ = action; /* ACTION_SA_QUERY_REQ/RESP */
  1391. LE_WRITE_2(frm, ni->ni_sa_query_trid); frm += 2;
  1392. m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
  1393. return m;
  1394. }
  1395. struct mbuf *
  1396. ieee80211_get_action(struct ieee80211com *ic, struct ieee80211_node *ni,
  1397. u_int8_t categ, u_int8_t action, int arg)
  1398. {
  1399. struct mbuf *m = NULL;
  1400. switch (categ) {
  1401. #ifndef IEEE80211_NO_HT
  1402. case IEEE80211_CATEG_BA:
  1403. switch (action) {
  1404. case IEEE80211_ACTION_ADDBA_REQ:
  1405. m = ieee80211_get_addba_req(ic, ni, arg & 0xffff);
  1406. break;
  1407. case IEEE80211_ACTION_ADDBA_RESP:
  1408. m = ieee80211_get_addba_resp(ic, ni, arg & 0xff,
  1409. arg >> 8, arg >> 16);
  1410. break;
  1411. case IEEE80211_ACTION_DELBA:
  1412. m = ieee80211_get_delba(ic, ni, arg & 0xff, arg >> 8,
  1413. arg >> 16);
  1414. break;
  1415. }
  1416. break;
  1417. #endif
  1418. case IEEE80211_CATEG_SA_QUERY:
  1419. switch (action) {
  1420. #ifndef IEEE80211_STA_ONLY
  1421. case IEEE80211_ACTION_SA_QUERY_REQ:
  1422. #endif
  1423. case IEEE80211_ACTION_SA_QUERY_RESP:
  1424. m = ieee80211_get_sa_query(ic, ni, action);
  1425. break;
  1426. }
  1427. break;
  1428. }
  1429. return m;
  1430. }
  1431. /*
  1432. * Send a management frame. The node is for the destination (or ic_bss
  1433. * when in station mode). Nodes other than ic_bss have their reference
  1434. * count bumped to reflect our use for an indeterminant time.
  1435. */
  1436. int
  1437. ieee80211_send_mgmt(struct ieee80211com *ic, struct ieee80211_node *ni,
  1438. int type, int arg1, int arg2)
  1439. {
  1440. #define senderr(_x, _v) do { ic->ic_stats._v++; ret = _x; goto bad; } while (0)
  1441. struct ifnet *ifp = &ic->ic_if;
  1442. struct mbuf *m;
  1443. int ret, timer;
  1444. if (ni == NULL)
  1445. panic("null node");
  1446. /*
  1447. * Hold a reference on the node so it doesn't go away until after
  1448. * the xmit is complete all the way in the driver. On error we
  1449. * will remove our reference.
  1450. */
  1451. ieee80211_ref_node(ni);
  1452. timer = 0;
  1453. switch (type) {
  1454. case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
  1455. if ((m = ieee80211_get_probe_req(ic, ni)) == NULL)
  1456. senderr(ENOMEM, is_tx_nombuf);
  1457. timer = IEEE80211_TRANS_WAIT;
  1458. break;
  1459. #ifndef IEEE80211_STA_ONLY
  1460. case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
  1461. if ((m = ieee80211_get_probe_resp(ic, ni)) == NULL)
  1462. senderr(ENOMEM, is_tx_nombuf);
  1463. break;
  1464. #endif
  1465. case IEEE80211_FC0_SUBTYPE_AUTH:
  1466. m = ieee80211_get_auth(ic, ni, arg1 >> 16, arg1 & 0xffff);
  1467. if (m == NULL)
  1468. senderr(ENOMEM, is_tx_nombuf);
  1469. if (ic->ic_opmode == IEEE80211_M_STA)
  1470. timer = IEEE80211_TRANS_WAIT;
  1471. break;
  1472. case IEEE80211_FC0_SUBTYPE_DEAUTH:
  1473. if ((m = ieee80211_get_deauth(ic, ni, arg1)) == NULL)
  1474. senderr(ENOMEM, is_tx_nombuf);
  1475. if (ifp->if_flags & IFF_DEBUG) {
  1476. printf("%s: station %s deauthenticate (reason %d)\n",
  1477. ifp->if_xname, ether_sprintf(ni->ni_macaddr),
  1478. arg1);
  1479. }
  1480. break;
  1481. case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
  1482. case IEEE80211_FC0_SUBTYPE_REASSOC_REQ:
  1483. if ((m = ieee80211_get_assoc_req(ic, ni, type)) == NULL)
  1484. senderr(ENOMEM, is_tx_nombuf);
  1485. timer = IEEE80211_TRANS_WAIT;
  1486. break;
  1487. #ifndef IEEE80211_STA_ONLY
  1488. case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
  1489. case IEEE80211_FC0_SUBTYPE_REASSOC_RESP:
  1490. if ((m = ieee80211_get_assoc_resp(ic, ni, arg1)) == NULL)
  1491. senderr(ENOMEM, is_tx_nombuf);
  1492. break;
  1493. #endif
  1494. case IEEE80211_FC0_SUBTYPE_DISASSOC:
  1495. if ((m = ieee80211_get_disassoc(ic, ni, arg1)) == NULL)
  1496. senderr(ENOMEM, is_tx_nombuf);
  1497. if (ifp->if_flags & IFF_DEBUG) {
  1498. printf("%s: station %s disassociate (reason %d)\n",
  1499. ifp->if_xname, ether_sprintf(ni->ni_macaddr),
  1500. arg1);
  1501. }
  1502. break;
  1503. case IEEE80211_FC0_SUBTYPE_ACTION:
  1504. m = ieee80211_get_action(ic, ni, arg1 >> 16, arg1 & 0xffff,
  1505. arg2);
  1506. if (m == NULL)
  1507. senderr(ENOMEM, is_tx_nombuf);
  1508. break;
  1509. default:
  1510. DPRINTF(("invalid mgmt frame type %u\n", type));
  1511. senderr(EINVAL, is_tx_unknownmgt);
  1512. /* NOTREACHED */
  1513. }
  1514. ret = ieee80211_mgmt_output(ifp, ni, m, type);
  1515. if (ret == 0) {
  1516. if (timer)
  1517. ic->ic_mgt_timer = timer;
  1518. } else {
  1519. bad:
  1520. ieee80211_release_node(ic, ni);
  1521. }
  1522. return ret;
  1523. #undef senderr
  1524. }
  1525. /*
  1526. * Build a RTS (Request To Send) control frame (see 7.2.1.1).
  1527. */
  1528. struct mbuf *
  1529. ieee80211_get_rts(struct ieee80211com *ic, const struct ieee80211_frame *wh,
  1530. u_int16_t dur)
  1531. {
  1532. struct ieee80211_frame_rts *rts;
  1533. struct mbuf *m;
  1534. MGETHDR(m, M_DONTWAIT, MT_DATA);
  1535. if (m == NULL)
  1536. return NULL;
  1537. m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
  1538. rts = mtod(m, struct ieee80211_frame_rts *);
  1539. rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
  1540. IEEE80211_FC0_SUBTYPE_RTS;
  1541. rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
  1542. *(u_int16_t *)rts->i_dur = htole16(dur);
  1543. IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
  1544. IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
  1545. return m;
  1546. }
  1547. /*
  1548. * Build a CTS-to-self (Clear To Send) control frame (see 7.2.1.2).
  1549. */
  1550. struct mbuf *
  1551. ieee80211_get_cts_to_self(struct ieee80211com *ic, u_int16_t dur)
  1552. {
  1553. struct ieee80211_frame_cts *cts;
  1554. struct mbuf *m;
  1555. MGETHDR(m, M_DONTWAIT, MT_DATA);
  1556. if (m == NULL)
  1557. return NULL;
  1558. m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_cts);
  1559. cts = mtod(m, struct ieee80211_frame_cts *);
  1560. cts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
  1561. IEEE80211_FC0_SUBTYPE_CTS;
  1562. cts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
  1563. *(u_int16_t *)cts->i_dur = htole16(dur);
  1564. IEEE80211_ADDR_COPY(cts->i_ra, ic->ic_myaddr);
  1565. return m;
  1566. }
  1567. #ifndef IEEE80211_STA_ONLY
  1568. /*-
  1569. * Beacon frame format:
  1570. * [8] Timestamp
  1571. * [2] Beacon interval
  1572. * [2] Capability
  1573. * [tlv] Service Set Identifier (SSID)
  1574. * [tlv] Supported rates
  1575. * [tlv] DS Parameter Set (802.11g)
  1576. * [tlv] IBSS Parameter Set
  1577. * [tlv] Traffic Indication Map (TIM)
  1578. * [tlv] ERP Information (802.11g)
  1579. * [tlv] Extended Supported Rates (802.11g)
  1580. * [tlv] RSN (802.11i)
  1581. * [tlv] EDCA Parameter Set (802.11e)
  1582. * [tlv] HT Capabilities (802.11n)
  1583. * [tlv] HT Operation (802.11n)
  1584. */
  1585. struct mbuf *
  1586. ieee80211_beacon_alloc(struct ieee80211com *ic, struct ieee80211_node *ni)
  1587. {
  1588. const struct ieee80211_rateset *rs = &ni->ni_rates;
  1589. struct ieee80211_frame *wh;
  1590. struct mbuf *m;
  1591. u_int8_t *frm;
  1592. m = ieee80211_getmgmt(M_DONTWAIT, MT_DATA,
  1593. 8 + 2 + 2 +
  1594. 2 + ((ic->ic_flags & IEEE80211_F_HIDENWID) ? 0 : ni->ni_esslen) +
  1595. 2 + min(rs->rs_nrates, IEEE80211_RATE_SIZE) +
  1596. 2 + 1 +
  1597. 2 + ((ic->ic_opmode == IEEE80211_M_IBSS) ? 2 : 254) +
  1598. ((ic->ic_curmode == IEEE80211_MODE_11G) ? 2 + 1 : 0) +
  1599. ((rs->rs_nrates > IEEE80211_RATE_SIZE) ?
  1600. 2 + rs->rs_nrates - IEEE80211_RATE_SIZE : 0) +
  1601. (((ic->ic_flags & IEEE80211_F_RSNON) &&
  1602. (ni->ni_rsnprotos & IEEE80211_PROTO_RSN)) ?
  1603. 2 + IEEE80211_RSNIE_MAXLEN : 0) +
  1604. ((ic->ic_flags & IEEE80211_F_QOS) ? 2 + 18 : 0) +
  1605. (((ic->ic_flags & IEEE80211_F_RSNON) &&
  1606. (ni->ni_rsnprotos & IEEE80211_PROTO_WPA)) ?
  1607. 2 + IEEE80211_WPAIE_MAXLEN : 0) +
  1608. ((ic->ic_flags & IEEE80211_F_HTON) ? 28 + 24 : 0));
  1609. if (m == NULL)
  1610. return NULL;
  1611. M_PREPEND(m, sizeof(struct ieee80211_frame), M_DONTWAIT);
  1612. if (m == NULL)
  1613. return NULL;
  1614. wh = mtod(m, struct ieee80211_frame *);
  1615. wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
  1616. IEEE80211_FC0_SUBTYPE_BEACON;
  1617. wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
  1618. *(u_int16_t *)wh->i_dur = 0;
  1619. IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
  1620. IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
  1621. IEEE80211_ADDR_COPY(wh->i_addr3, ni->ni_bssid);
  1622. *(u_int16_t *)wh->i_seq = 0;
  1623. frm = (u_int8_t *)&wh[1];
  1624. memset(frm, 0, 8); frm += 8; /* timestamp is set by hardware */
  1625. LE_WRITE_2(frm, ni->ni_intval); frm += 2;
  1626. frm = ieee80211_add_capinfo(frm, ic, ni);
  1627. if (ic->ic_flags & IEEE80211_F_HIDENWID)
  1628. frm = ieee80211_add_ssid(frm, NULL, 0);
  1629. else
  1630. frm = ieee80211_add_ssid(frm, ni->ni_essid, ni->ni_esslen);
  1631. frm = ieee80211_add_rates(frm, rs);
  1632. frm = ieee80211_add_ds_params(frm, ic, ni);
  1633. if (ic->ic_opmode == IEEE80211_M_IBSS)
  1634. frm = ieee80211_add_ibss_params(frm, ni);
  1635. else
  1636. frm = ieee80211_add_tim(frm, ic);
  1637. if (ic->ic_curmode == IEEE80211_MODE_11G)
  1638. frm = ieee80211_add_erp(frm, ic);
  1639. if (rs->rs_nrates > IEEE80211_RATE_SIZE)
  1640. frm = ieee80211_add_xrates(frm, rs);
  1641. if ((ic->ic_flags & IEEE80211_F_RSNON) &&
  1642. (ni->ni_rsnprotos & IEEE80211_PROTO_RSN))
  1643. frm = ieee80211_add_rsn(frm, ic, ni);
  1644. if (ic->ic_flags & IEEE80211_F_QOS)
  1645. frm = ieee80211_add_edca_params(frm, ic);
  1646. if ((ic->ic_flags & IEEE80211_F_RSNON) &&
  1647. (ni->ni_rsnprotos & IEEE80211_PROTO_WPA))
  1648. frm = ieee80211_add_wpa(frm, ic, ni);
  1649. #ifndef IEEE80211_NO_HT
  1650. if (ic->ic_flags & IEEE80211_F_HTON) {
  1651. frm = ieee80211_add_htcaps(frm, ic);
  1652. frm = ieee80211_add_htop(frm, ic);
  1653. }
  1654. #endif
  1655. m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
  1656. m->m_pkthdr.ph_cookie = ni;
  1657. return m;
  1658. }
  1659. /*
  1660. * Check if an outgoing MSDU or management frame should be buffered into
  1661. * the AP for power management. Return 1 if the frame was buffered into
  1662. * the AP, or 0 if the frame shall be transmitted immediately.
  1663. */
  1664. int
  1665. ieee80211_pwrsave(struct ieee80211com *ic, struct mbuf *m,
  1666. struct ieee80211_node *ni)
  1667. {
  1668. const struct ieee80211_frame *wh;
  1669. KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP);
  1670. if (!(ic->ic_caps & IEEE80211_C_APPMGT))
  1671. return 0;
  1672. wh = mtod(m, struct ieee80211_frame *);
  1673. if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
  1674. /*
  1675. * Buffer group addressed MSDUs with the Order bit clear
  1676. * if any associated STAs are in PS mode.
  1677. */
  1678. if ((wh->i_fc[1] & IEEE80211_FC1_ORDER) ||
  1679. ic->ic_pssta == 0)
  1680. return 0;
  1681. ic->ic_tim_mcast_pending = 1;
  1682. } else {
  1683. /*
  1684. * Buffer MSDUs, A-MSDUs or management frames destined for
  1685. * PS STAs.
  1686. */
  1687. if (ni->ni_pwrsave == IEEE80211_PS_AWAKE ||
  1688. (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
  1689. IEEE80211_FC0_TYPE_CTL)
  1690. return 0;
  1691. if (IF_IS_EMPTY(&ni->ni_savedq))
  1692. (*ic->ic_set_tim)(ic, ni->ni_associd, 1);
  1693. }
  1694. /* NB: ni == ic->ic_bss for broadcast/multicast */
  1695. if (IF_QFULL(&ni->ni_savedq)) {
  1696. /* XXX should we drop the oldest instead? */
  1697. IF_DROP(&ni->ni_savedq);
  1698. m_freem(m);
  1699. } else {
  1700. IF_ENQUEUE(&ni->ni_savedq, m);
  1701. /*
  1702. * Similar to ieee80211_mgmt_output, store the node in a
  1703. * special pkthdr field.
  1704. */
  1705. m->m_pkthdr.ph_cookie = ni;
  1706. }
  1707. return 1;
  1708. }
  1709. #endif /* IEEE80211_STA_ONLY */