util.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Wireless utility functions
  4. *
  5. * Copyright 2007-2009 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. */
  8. #include <linux/export.h>
  9. #include <linux/bitops.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/slab.h>
  12. #include <net/cfg80211.h>
  13. #include <net/ip.h>
  14. #include <net/dsfield.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/mpls.h>
  17. #include <linux/gcd.h>
  18. #include "core.h"
  19. #include "rdev-ops.h"
  20. struct ieee80211_rate *
  21. ieee80211_get_response_rate(struct ieee80211_supported_band *sband,
  22. u32 basic_rates, int bitrate)
  23. {
  24. struct ieee80211_rate *result = &sband->bitrates[0];
  25. int i;
  26. for (i = 0; i < sband->n_bitrates; i++) {
  27. if (!(basic_rates & BIT(i)))
  28. continue;
  29. if (sband->bitrates[i].bitrate > bitrate)
  30. continue;
  31. result = &sband->bitrates[i];
  32. }
  33. return result;
  34. }
  35. EXPORT_SYMBOL(ieee80211_get_response_rate);
  36. u32 ieee80211_mandatory_rates(struct ieee80211_supported_band *sband,
  37. enum nl80211_bss_scan_width scan_width)
  38. {
  39. struct ieee80211_rate *bitrates;
  40. u32 mandatory_rates = 0;
  41. enum ieee80211_rate_flags mandatory_flag;
  42. int i;
  43. if (WARN_ON(!sband))
  44. return 1;
  45. if (sband->band == NL80211_BAND_2GHZ) {
  46. if (scan_width == NL80211_BSS_CHAN_WIDTH_5 ||
  47. scan_width == NL80211_BSS_CHAN_WIDTH_10)
  48. mandatory_flag = IEEE80211_RATE_MANDATORY_G;
  49. else
  50. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  51. } else {
  52. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  53. }
  54. bitrates = sband->bitrates;
  55. for (i = 0; i < sband->n_bitrates; i++)
  56. if (bitrates[i].flags & mandatory_flag)
  57. mandatory_rates |= BIT(i);
  58. return mandatory_rates;
  59. }
  60. EXPORT_SYMBOL(ieee80211_mandatory_rates);
  61. int ieee80211_channel_to_frequency(int chan, enum nl80211_band band)
  62. {
  63. /* see 802.11 17.3.8.3.2 and Annex J
  64. * there are overlapping channel numbers in 5GHz and 2GHz bands */
  65. if (chan <= 0)
  66. return 0; /* not supported */
  67. switch (band) {
  68. case NL80211_BAND_2GHZ:
  69. if (chan == 14)
  70. return 2484;
  71. else if (chan < 14)
  72. return 2407 + chan * 5;
  73. break;
  74. case NL80211_BAND_5GHZ:
  75. if (chan >= 182 && chan <= 196)
  76. return 4000 + chan * 5;
  77. else
  78. return 5000 + chan * 5;
  79. break;
  80. case NL80211_BAND_60GHZ:
  81. if (chan < 5)
  82. return 56160 + chan * 2160;
  83. break;
  84. default:
  85. ;
  86. }
  87. return 0; /* not supported */
  88. }
  89. EXPORT_SYMBOL(ieee80211_channel_to_frequency);
  90. int ieee80211_frequency_to_channel(int freq)
  91. {
  92. /* see 802.11 17.3.8.3.2 and Annex J */
  93. if (freq == 2484)
  94. return 14;
  95. else if (freq < 2484)
  96. return (freq - 2407) / 5;
  97. else if (freq >= 4910 && freq <= 4980)
  98. return (freq - 4000) / 5;
  99. else if (freq <= 45000) /* DMG band lower limit */
  100. return (freq - 5000) / 5;
  101. else if (freq >= 58320 && freq <= 64800)
  102. return (freq - 56160) / 2160;
  103. else
  104. return 0;
  105. }
  106. EXPORT_SYMBOL(ieee80211_frequency_to_channel);
  107. struct ieee80211_channel *ieee80211_get_channel(struct wiphy *wiphy, int freq)
  108. {
  109. enum nl80211_band band;
  110. struct ieee80211_supported_band *sband;
  111. int i;
  112. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  113. sband = wiphy->bands[band];
  114. if (!sband)
  115. continue;
  116. for (i = 0; i < sband->n_channels; i++) {
  117. if (sband->channels[i].center_freq == freq)
  118. return &sband->channels[i];
  119. }
  120. }
  121. return NULL;
  122. }
  123. EXPORT_SYMBOL(ieee80211_get_channel);
  124. static void set_mandatory_flags_band(struct ieee80211_supported_band *sband)
  125. {
  126. int i, want;
  127. switch (sband->band) {
  128. case NL80211_BAND_5GHZ:
  129. want = 3;
  130. for (i = 0; i < sband->n_bitrates; i++) {
  131. if (sband->bitrates[i].bitrate == 60 ||
  132. sband->bitrates[i].bitrate == 120 ||
  133. sband->bitrates[i].bitrate == 240) {
  134. sband->bitrates[i].flags |=
  135. IEEE80211_RATE_MANDATORY_A;
  136. want--;
  137. }
  138. }
  139. WARN_ON(want);
  140. break;
  141. case NL80211_BAND_2GHZ:
  142. want = 7;
  143. for (i = 0; i < sband->n_bitrates; i++) {
  144. if (sband->bitrates[i].bitrate == 10) {
  145. sband->bitrates[i].flags |=
  146. IEEE80211_RATE_MANDATORY_B |
  147. IEEE80211_RATE_MANDATORY_G;
  148. want--;
  149. }
  150. if (sband->bitrates[i].bitrate == 20 ||
  151. sband->bitrates[i].bitrate == 55 ||
  152. sband->bitrates[i].bitrate == 110 ||
  153. sband->bitrates[i].bitrate == 60 ||
  154. sband->bitrates[i].bitrate == 120 ||
  155. sband->bitrates[i].bitrate == 240) {
  156. sband->bitrates[i].flags |=
  157. IEEE80211_RATE_MANDATORY_G;
  158. want--;
  159. }
  160. if (sband->bitrates[i].bitrate != 10 &&
  161. sband->bitrates[i].bitrate != 20 &&
  162. sband->bitrates[i].bitrate != 55 &&
  163. sband->bitrates[i].bitrate != 110)
  164. sband->bitrates[i].flags |=
  165. IEEE80211_RATE_ERP_G;
  166. }
  167. WARN_ON(want != 0 && want != 3 && want != 6);
  168. break;
  169. case NL80211_BAND_60GHZ:
  170. /* check for mandatory HT MCS 1..4 */
  171. WARN_ON(!sband->ht_cap.ht_supported);
  172. WARN_ON((sband->ht_cap.mcs.rx_mask[0] & 0x1e) != 0x1e);
  173. break;
  174. case NUM_NL80211_BANDS:
  175. default:
  176. WARN_ON(1);
  177. break;
  178. }
  179. }
  180. void ieee80211_set_bitrate_flags(struct wiphy *wiphy)
  181. {
  182. enum nl80211_band band;
  183. for (band = 0; band < NUM_NL80211_BANDS; band++)
  184. if (wiphy->bands[band])
  185. set_mandatory_flags_band(wiphy->bands[band]);
  186. }
  187. bool cfg80211_supported_cipher_suite(struct wiphy *wiphy, u32 cipher)
  188. {
  189. int i;
  190. for (i = 0; i < wiphy->n_cipher_suites; i++)
  191. if (cipher == wiphy->cipher_suites[i])
  192. return true;
  193. return false;
  194. }
  195. int cfg80211_validate_key_settings(struct cfg80211_registered_device *rdev,
  196. struct key_params *params, int key_idx,
  197. bool pairwise, const u8 *mac_addr)
  198. {
  199. if (key_idx < 0 || key_idx > 5)
  200. return -EINVAL;
  201. if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
  202. return -EINVAL;
  203. if (pairwise && !mac_addr)
  204. return -EINVAL;
  205. switch (params->cipher) {
  206. case WLAN_CIPHER_SUITE_TKIP:
  207. case WLAN_CIPHER_SUITE_CCMP:
  208. case WLAN_CIPHER_SUITE_CCMP_256:
  209. case WLAN_CIPHER_SUITE_GCMP:
  210. case WLAN_CIPHER_SUITE_GCMP_256:
  211. /* Disallow pairwise keys with non-zero index unless it's WEP
  212. * or a vendor specific cipher (because current deployments use
  213. * pairwise WEP keys with non-zero indices and for vendor
  214. * specific ciphers this should be validated in the driver or
  215. * hardware level - but 802.11i clearly specifies to use zero)
  216. */
  217. if (pairwise && key_idx)
  218. return -EINVAL;
  219. break;
  220. case WLAN_CIPHER_SUITE_AES_CMAC:
  221. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  222. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  223. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  224. /* Disallow BIP (group-only) cipher as pairwise cipher */
  225. if (pairwise)
  226. return -EINVAL;
  227. if (key_idx < 4)
  228. return -EINVAL;
  229. break;
  230. case WLAN_CIPHER_SUITE_WEP40:
  231. case WLAN_CIPHER_SUITE_WEP104:
  232. if (key_idx > 3)
  233. return -EINVAL;
  234. default:
  235. break;
  236. }
  237. switch (params->cipher) {
  238. case WLAN_CIPHER_SUITE_WEP40:
  239. if (params->key_len != WLAN_KEY_LEN_WEP40)
  240. return -EINVAL;
  241. break;
  242. case WLAN_CIPHER_SUITE_TKIP:
  243. if (params->key_len != WLAN_KEY_LEN_TKIP)
  244. return -EINVAL;
  245. break;
  246. case WLAN_CIPHER_SUITE_CCMP:
  247. if (params->key_len != WLAN_KEY_LEN_CCMP)
  248. return -EINVAL;
  249. break;
  250. case WLAN_CIPHER_SUITE_CCMP_256:
  251. if (params->key_len != WLAN_KEY_LEN_CCMP_256)
  252. return -EINVAL;
  253. break;
  254. case WLAN_CIPHER_SUITE_GCMP:
  255. if (params->key_len != WLAN_KEY_LEN_GCMP)
  256. return -EINVAL;
  257. break;
  258. case WLAN_CIPHER_SUITE_GCMP_256:
  259. if (params->key_len != WLAN_KEY_LEN_GCMP_256)
  260. return -EINVAL;
  261. break;
  262. case WLAN_CIPHER_SUITE_WEP104:
  263. if (params->key_len != WLAN_KEY_LEN_WEP104)
  264. return -EINVAL;
  265. break;
  266. case WLAN_CIPHER_SUITE_AES_CMAC:
  267. if (params->key_len != WLAN_KEY_LEN_AES_CMAC)
  268. return -EINVAL;
  269. break;
  270. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  271. if (params->key_len != WLAN_KEY_LEN_BIP_CMAC_256)
  272. return -EINVAL;
  273. break;
  274. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  275. if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_128)
  276. return -EINVAL;
  277. break;
  278. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  279. if (params->key_len != WLAN_KEY_LEN_BIP_GMAC_256)
  280. return -EINVAL;
  281. break;
  282. default:
  283. /*
  284. * We don't know anything about this algorithm,
  285. * allow using it -- but the driver must check
  286. * all parameters! We still check below whether
  287. * or not the driver supports this algorithm,
  288. * of course.
  289. */
  290. break;
  291. }
  292. if (params->seq) {
  293. switch (params->cipher) {
  294. case WLAN_CIPHER_SUITE_WEP40:
  295. case WLAN_CIPHER_SUITE_WEP104:
  296. /* These ciphers do not use key sequence */
  297. return -EINVAL;
  298. case WLAN_CIPHER_SUITE_TKIP:
  299. case WLAN_CIPHER_SUITE_CCMP:
  300. case WLAN_CIPHER_SUITE_CCMP_256:
  301. case WLAN_CIPHER_SUITE_GCMP:
  302. case WLAN_CIPHER_SUITE_GCMP_256:
  303. case WLAN_CIPHER_SUITE_AES_CMAC:
  304. case WLAN_CIPHER_SUITE_BIP_CMAC_256:
  305. case WLAN_CIPHER_SUITE_BIP_GMAC_128:
  306. case WLAN_CIPHER_SUITE_BIP_GMAC_256:
  307. if (params->seq_len != 6)
  308. return -EINVAL;
  309. break;
  310. }
  311. }
  312. if (!cfg80211_supported_cipher_suite(&rdev->wiphy, params->cipher))
  313. return -EINVAL;
  314. return 0;
  315. }
  316. unsigned int __attribute_const__ ieee80211_hdrlen(__le16 fc)
  317. {
  318. unsigned int hdrlen = 24;
  319. if (ieee80211_is_data(fc)) {
  320. if (ieee80211_has_a4(fc))
  321. hdrlen = 30;
  322. if (ieee80211_is_data_qos(fc)) {
  323. hdrlen += IEEE80211_QOS_CTL_LEN;
  324. if (ieee80211_has_order(fc))
  325. hdrlen += IEEE80211_HT_CTL_LEN;
  326. }
  327. goto out;
  328. }
  329. if (ieee80211_is_mgmt(fc)) {
  330. if (ieee80211_has_order(fc))
  331. hdrlen += IEEE80211_HT_CTL_LEN;
  332. goto out;
  333. }
  334. if (ieee80211_is_ctl(fc)) {
  335. /*
  336. * ACK and CTS are 10 bytes, all others 16. To see how
  337. * to get this condition consider
  338. * subtype mask: 0b0000000011110000 (0x00F0)
  339. * ACK subtype: 0b0000000011010000 (0x00D0)
  340. * CTS subtype: 0b0000000011000000 (0x00C0)
  341. * bits that matter: ^^^ (0x00E0)
  342. * value of those: 0b0000000011000000 (0x00C0)
  343. */
  344. if ((fc & cpu_to_le16(0x00E0)) == cpu_to_le16(0x00C0))
  345. hdrlen = 10;
  346. else
  347. hdrlen = 16;
  348. }
  349. out:
  350. return hdrlen;
  351. }
  352. EXPORT_SYMBOL(ieee80211_hdrlen);
  353. unsigned int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  354. {
  355. const struct ieee80211_hdr *hdr =
  356. (const struct ieee80211_hdr *)skb->data;
  357. unsigned int hdrlen;
  358. if (unlikely(skb->len < 10))
  359. return 0;
  360. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  361. if (unlikely(hdrlen > skb->len))
  362. return 0;
  363. return hdrlen;
  364. }
  365. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  366. static unsigned int __ieee80211_get_mesh_hdrlen(u8 flags)
  367. {
  368. int ae = flags & MESH_FLAGS_AE;
  369. /* 802.11-2012, 8.2.4.7.3 */
  370. switch (ae) {
  371. default:
  372. case 0:
  373. return 6;
  374. case MESH_FLAGS_AE_A4:
  375. return 12;
  376. case MESH_FLAGS_AE_A5_A6:
  377. return 18;
  378. }
  379. }
  380. unsigned int ieee80211_get_mesh_hdrlen(struct ieee80211s_hdr *meshhdr)
  381. {
  382. return __ieee80211_get_mesh_hdrlen(meshhdr->flags);
  383. }
  384. EXPORT_SYMBOL(ieee80211_get_mesh_hdrlen);
  385. int ieee80211_data_to_8023_exthdr(struct sk_buff *skb, struct ethhdr *ehdr,
  386. const u8 *addr, enum nl80211_iftype iftype,
  387. bool is_amsdu)
  388. {
  389. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  390. struct {
  391. u8 hdr[ETH_ALEN] __aligned(2);
  392. __be16 proto;
  393. } payload;
  394. struct ethhdr tmp;
  395. u16 hdrlen;
  396. u8 mesh_flags = 0;
  397. if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
  398. return -1;
  399. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  400. if (skb->len < hdrlen + 8)
  401. return -1;
  402. /* convert IEEE 802.11 header + possible LLC headers into Ethernet
  403. * header
  404. * IEEE 802.11 address fields:
  405. * ToDS FromDS Addr1 Addr2 Addr3 Addr4
  406. * 0 0 DA SA BSSID n/a
  407. * 0 1 DA BSSID SA n/a
  408. * 1 0 BSSID SA DA n/a
  409. * 1 1 RA TA DA SA
  410. */
  411. memcpy(tmp.h_dest, ieee80211_get_DA(hdr), ETH_ALEN);
  412. memcpy(tmp.h_source, ieee80211_get_SA(hdr), ETH_ALEN);
  413. if (iftype == NL80211_IFTYPE_MESH_POINT)
  414. skb_copy_bits(skb, hdrlen, &mesh_flags, 1);
  415. mesh_flags &= MESH_FLAGS_AE;
  416. switch (hdr->frame_control &
  417. cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  418. case cpu_to_le16(IEEE80211_FCTL_TODS):
  419. if (unlikely(iftype != NL80211_IFTYPE_AP &&
  420. iftype != NL80211_IFTYPE_AP_VLAN &&
  421. iftype != NL80211_IFTYPE_P2P_GO))
  422. return -1;
  423. break;
  424. case cpu_to_le16(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  425. if (unlikely(iftype != NL80211_IFTYPE_WDS &&
  426. iftype != NL80211_IFTYPE_MESH_POINT &&
  427. iftype != NL80211_IFTYPE_AP_VLAN &&
  428. iftype != NL80211_IFTYPE_STATION))
  429. return -1;
  430. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  431. if (mesh_flags == MESH_FLAGS_AE_A4)
  432. return -1;
  433. if (mesh_flags == MESH_FLAGS_AE_A5_A6) {
  434. skb_copy_bits(skb, hdrlen +
  435. offsetof(struct ieee80211s_hdr, eaddr1),
  436. tmp.h_dest, 2 * ETH_ALEN);
  437. }
  438. hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
  439. }
  440. break;
  441. case cpu_to_le16(IEEE80211_FCTL_FROMDS):
  442. if ((iftype != NL80211_IFTYPE_STATION &&
  443. iftype != NL80211_IFTYPE_P2P_CLIENT &&
  444. iftype != NL80211_IFTYPE_MESH_POINT) ||
  445. (is_multicast_ether_addr(tmp.h_dest) &&
  446. ether_addr_equal(tmp.h_source, addr)))
  447. return -1;
  448. if (iftype == NL80211_IFTYPE_MESH_POINT) {
  449. if (mesh_flags == MESH_FLAGS_AE_A5_A6)
  450. return -1;
  451. if (mesh_flags == MESH_FLAGS_AE_A4)
  452. skb_copy_bits(skb, hdrlen +
  453. offsetof(struct ieee80211s_hdr, eaddr1),
  454. tmp.h_source, ETH_ALEN);
  455. hdrlen += __ieee80211_get_mesh_hdrlen(mesh_flags);
  456. }
  457. break;
  458. case cpu_to_le16(0):
  459. if (iftype != NL80211_IFTYPE_ADHOC &&
  460. iftype != NL80211_IFTYPE_STATION &&
  461. iftype != NL80211_IFTYPE_OCB)
  462. return -1;
  463. break;
  464. }
  465. skb_copy_bits(skb, hdrlen, &payload, sizeof(payload));
  466. tmp.h_proto = payload.proto;
  467. if (likely((!is_amsdu && ether_addr_equal(payload.hdr, rfc1042_header) &&
  468. tmp.h_proto != htons(ETH_P_AARP) &&
  469. tmp.h_proto != htons(ETH_P_IPX)) ||
  470. ether_addr_equal(payload.hdr, bridge_tunnel_header)))
  471. /* remove RFC1042 or Bridge-Tunnel encapsulation and
  472. * replace EtherType */
  473. hdrlen += ETH_ALEN + 2;
  474. else
  475. tmp.h_proto = htons(skb->len - hdrlen);
  476. pskb_pull(skb, hdrlen);
  477. if (!ehdr)
  478. ehdr = skb_push(skb, sizeof(struct ethhdr));
  479. memcpy(ehdr, &tmp, sizeof(tmp));
  480. return 0;
  481. }
  482. EXPORT_SYMBOL(ieee80211_data_to_8023_exthdr);
  483. int ieee80211_data_from_8023(struct sk_buff *skb, const u8 *addr,
  484. enum nl80211_iftype iftype,
  485. const u8 *bssid, bool qos)
  486. {
  487. struct ieee80211_hdr hdr;
  488. u16 hdrlen, ethertype;
  489. __le16 fc;
  490. const u8 *encaps_data;
  491. int encaps_len, skip_header_bytes;
  492. int nh_pos, h_pos;
  493. int head_need;
  494. if (unlikely(skb->len < ETH_HLEN))
  495. return -EINVAL;
  496. nh_pos = skb_network_header(skb) - skb->data;
  497. h_pos = skb_transport_header(skb) - skb->data;
  498. /* convert Ethernet header to proper 802.11 header (based on
  499. * operation mode) */
  500. ethertype = (skb->data[12] << 8) | skb->data[13];
  501. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
  502. switch (iftype) {
  503. case NL80211_IFTYPE_AP:
  504. case NL80211_IFTYPE_AP_VLAN:
  505. case NL80211_IFTYPE_P2P_GO:
  506. fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  507. /* DA BSSID SA */
  508. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  509. memcpy(hdr.addr2, addr, ETH_ALEN);
  510. memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
  511. hdrlen = 24;
  512. break;
  513. case NL80211_IFTYPE_STATION:
  514. case NL80211_IFTYPE_P2P_CLIENT:
  515. fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
  516. /* BSSID SA DA */
  517. memcpy(hdr.addr1, bssid, ETH_ALEN);
  518. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  519. memcpy(hdr.addr3, skb->data, ETH_ALEN);
  520. hdrlen = 24;
  521. break;
  522. case NL80211_IFTYPE_OCB:
  523. case NL80211_IFTYPE_ADHOC:
  524. /* DA SA BSSID */
  525. memcpy(hdr.addr1, skb->data, ETH_ALEN);
  526. memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
  527. memcpy(hdr.addr3, bssid, ETH_ALEN);
  528. hdrlen = 24;
  529. break;
  530. default:
  531. return -EOPNOTSUPP;
  532. }
  533. if (qos) {
  534. fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  535. hdrlen += 2;
  536. }
  537. hdr.frame_control = fc;
  538. hdr.duration_id = 0;
  539. hdr.seq_ctrl = 0;
  540. skip_header_bytes = ETH_HLEN;
  541. if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
  542. encaps_data = bridge_tunnel_header;
  543. encaps_len = sizeof(bridge_tunnel_header);
  544. skip_header_bytes -= 2;
  545. } else if (ethertype >= ETH_P_802_3_MIN) {
  546. encaps_data = rfc1042_header;
  547. encaps_len = sizeof(rfc1042_header);
  548. skip_header_bytes -= 2;
  549. } else {
  550. encaps_data = NULL;
  551. encaps_len = 0;
  552. }
  553. skb_pull(skb, skip_header_bytes);
  554. nh_pos -= skip_header_bytes;
  555. h_pos -= skip_header_bytes;
  556. head_need = hdrlen + encaps_len - skb_headroom(skb);
  557. if (head_need > 0 || skb_cloned(skb)) {
  558. head_need = max(head_need, 0);
  559. if (head_need)
  560. skb_orphan(skb);
  561. if (pskb_expand_head(skb, head_need, 0, GFP_ATOMIC))
  562. return -ENOMEM;
  563. }
  564. if (encaps_data) {
  565. memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
  566. nh_pos += encaps_len;
  567. h_pos += encaps_len;
  568. }
  569. memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
  570. nh_pos += hdrlen;
  571. h_pos += hdrlen;
  572. /* Update skb pointers to various headers since this modified frame
  573. * is going to go through Linux networking code that may potentially
  574. * need things like pointer to IP header. */
  575. skb_reset_mac_header(skb);
  576. skb_set_network_header(skb, nh_pos);
  577. skb_set_transport_header(skb, h_pos);
  578. return 0;
  579. }
  580. EXPORT_SYMBOL(ieee80211_data_from_8023);
  581. static void
  582. __frame_add_frag(struct sk_buff *skb, struct page *page,
  583. void *ptr, int len, int size)
  584. {
  585. struct skb_shared_info *sh = skb_shinfo(skb);
  586. int page_offset;
  587. get_page(page);
  588. page_offset = ptr - page_address(page);
  589. skb_add_rx_frag(skb, sh->nr_frags, page, page_offset, len, size);
  590. }
  591. static void
  592. __ieee80211_amsdu_copy_frag(struct sk_buff *skb, struct sk_buff *frame,
  593. int offset, int len)
  594. {
  595. struct skb_shared_info *sh = skb_shinfo(skb);
  596. const skb_frag_t *frag = &sh->frags[0];
  597. struct page *frag_page;
  598. void *frag_ptr;
  599. int frag_len, frag_size;
  600. int head_size = skb->len - skb->data_len;
  601. int cur_len;
  602. frag_page = virt_to_head_page(skb->head);
  603. frag_ptr = skb->data;
  604. frag_size = head_size;
  605. while (offset >= frag_size) {
  606. offset -= frag_size;
  607. frag_page = skb_frag_page(frag);
  608. frag_ptr = skb_frag_address(frag);
  609. frag_size = skb_frag_size(frag);
  610. frag++;
  611. }
  612. frag_ptr += offset;
  613. frag_len = frag_size - offset;
  614. cur_len = min(len, frag_len);
  615. __frame_add_frag(frame, frag_page, frag_ptr, cur_len, frag_size);
  616. len -= cur_len;
  617. while (len > 0) {
  618. frag_len = skb_frag_size(frag);
  619. cur_len = min(len, frag_len);
  620. __frame_add_frag(frame, skb_frag_page(frag),
  621. skb_frag_address(frag), cur_len, frag_len);
  622. len -= cur_len;
  623. frag++;
  624. }
  625. }
  626. static struct sk_buff *
  627. __ieee80211_amsdu_copy(struct sk_buff *skb, unsigned int hlen,
  628. int offset, int len, bool reuse_frag)
  629. {
  630. struct sk_buff *frame;
  631. int cur_len = len;
  632. if (skb->len - offset < len)
  633. return NULL;
  634. /*
  635. * When reusing framents, copy some data to the head to simplify
  636. * ethernet header handling and speed up protocol header processing
  637. * in the stack later.
  638. */
  639. if (reuse_frag)
  640. cur_len = min_t(int, len, 32);
  641. /*
  642. * Allocate and reserve two bytes more for payload
  643. * alignment since sizeof(struct ethhdr) is 14.
  644. */
  645. frame = dev_alloc_skb(hlen + sizeof(struct ethhdr) + 2 + cur_len);
  646. if (!frame)
  647. return NULL;
  648. skb_reserve(frame, hlen + sizeof(struct ethhdr) + 2);
  649. skb_copy_bits(skb, offset, skb_put(frame, cur_len), cur_len);
  650. len -= cur_len;
  651. if (!len)
  652. return frame;
  653. offset += cur_len;
  654. __ieee80211_amsdu_copy_frag(skb, frame, offset, len);
  655. return frame;
  656. }
  657. void ieee80211_amsdu_to_8023s(struct sk_buff *skb, struct sk_buff_head *list,
  658. const u8 *addr, enum nl80211_iftype iftype,
  659. const unsigned int extra_headroom,
  660. const u8 *check_da, const u8 *check_sa)
  661. {
  662. unsigned int hlen = ALIGN(extra_headroom, 4);
  663. struct sk_buff *frame = NULL;
  664. u16 ethertype;
  665. u8 *payload;
  666. int offset = 0, remaining;
  667. struct ethhdr eth;
  668. bool reuse_frag = skb->head_frag && !skb_has_frag_list(skb);
  669. bool reuse_skb = false;
  670. bool last = false;
  671. while (!last) {
  672. unsigned int subframe_len;
  673. int len;
  674. u8 padding;
  675. skb_copy_bits(skb, offset, &eth, sizeof(eth));
  676. len = ntohs(eth.h_proto);
  677. subframe_len = sizeof(struct ethhdr) + len;
  678. padding = (4 - subframe_len) & 0x3;
  679. /* the last MSDU has no padding */
  680. remaining = skb->len - offset;
  681. if (subframe_len > remaining)
  682. goto purge;
  683. /* mitigate A-MSDU aggregation injection attacks */
  684. if (ether_addr_equal(eth.h_dest, rfc1042_header))
  685. goto purge;
  686. offset += sizeof(struct ethhdr);
  687. last = remaining <= subframe_len + padding;
  688. /* FIXME: should we really accept multicast DA? */
  689. if ((check_da && !is_multicast_ether_addr(eth.h_dest) &&
  690. !ether_addr_equal(check_da, eth.h_dest)) ||
  691. (check_sa && !ether_addr_equal(check_sa, eth.h_source))) {
  692. offset += len + padding;
  693. continue;
  694. }
  695. /* reuse skb for the last subframe */
  696. if (!skb_is_nonlinear(skb) && !reuse_frag && last) {
  697. skb_pull(skb, offset);
  698. frame = skb;
  699. reuse_skb = true;
  700. } else {
  701. frame = __ieee80211_amsdu_copy(skb, hlen, offset, len,
  702. reuse_frag);
  703. if (!frame)
  704. goto purge;
  705. offset += len + padding;
  706. }
  707. skb_reset_network_header(frame);
  708. frame->dev = skb->dev;
  709. frame->priority = skb->priority;
  710. payload = frame->data;
  711. ethertype = (payload[6] << 8) | payload[7];
  712. if (likely((ether_addr_equal(payload, rfc1042_header) &&
  713. ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
  714. ether_addr_equal(payload, bridge_tunnel_header))) {
  715. eth.h_proto = htons(ethertype);
  716. skb_pull(frame, ETH_ALEN + 2);
  717. }
  718. memcpy(skb_push(frame, sizeof(eth)), &eth, sizeof(eth));
  719. __skb_queue_tail(list, frame);
  720. }
  721. if (!reuse_skb)
  722. dev_kfree_skb(skb);
  723. return;
  724. purge:
  725. __skb_queue_purge(list);
  726. dev_kfree_skb(skb);
  727. }
  728. EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
  729. /* Given a data frame determine the 802.1p/1d tag to use. */
  730. unsigned int cfg80211_classify8021d(struct sk_buff *skb,
  731. struct cfg80211_qos_map *qos_map)
  732. {
  733. unsigned int dscp;
  734. unsigned char vlan_priority;
  735. /* skb->priority values from 256->263 are magic values to
  736. * directly indicate a specific 802.1d priority. This is used
  737. * to allow 802.1d priority to be passed directly in from VLAN
  738. * tags, etc.
  739. */
  740. if (skb->priority >= 256 && skb->priority <= 263)
  741. return skb->priority - 256;
  742. if (skb_vlan_tag_present(skb)) {
  743. vlan_priority = (skb_vlan_tag_get(skb) & VLAN_PRIO_MASK)
  744. >> VLAN_PRIO_SHIFT;
  745. if (vlan_priority > 0)
  746. return vlan_priority;
  747. }
  748. switch (skb->protocol) {
  749. case htons(ETH_P_IP):
  750. dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
  751. break;
  752. case htons(ETH_P_IPV6):
  753. dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & 0xfc;
  754. break;
  755. case htons(ETH_P_MPLS_UC):
  756. case htons(ETH_P_MPLS_MC): {
  757. struct mpls_label mpls_tmp, *mpls;
  758. mpls = skb_header_pointer(skb, sizeof(struct ethhdr),
  759. sizeof(*mpls), &mpls_tmp);
  760. if (!mpls)
  761. return 0;
  762. return (ntohl(mpls->entry) & MPLS_LS_TC_MASK)
  763. >> MPLS_LS_TC_SHIFT;
  764. }
  765. case htons(ETH_P_80221):
  766. /* 802.21 is always network control traffic */
  767. return 7;
  768. default:
  769. return 0;
  770. }
  771. if (qos_map) {
  772. unsigned int i, tmp_dscp = dscp >> 2;
  773. for (i = 0; i < qos_map->num_des; i++) {
  774. if (tmp_dscp == qos_map->dscp_exception[i].dscp)
  775. return qos_map->dscp_exception[i].up;
  776. }
  777. for (i = 0; i < 8; i++) {
  778. if (tmp_dscp >= qos_map->up[i].low &&
  779. tmp_dscp <= qos_map->up[i].high)
  780. return i;
  781. }
  782. }
  783. return dscp >> 5;
  784. }
  785. EXPORT_SYMBOL(cfg80211_classify8021d);
  786. const u8 *ieee80211_bss_get_ie(struct cfg80211_bss *bss, u8 ie)
  787. {
  788. const struct cfg80211_bss_ies *ies;
  789. ies = rcu_dereference(bss->ies);
  790. if (!ies)
  791. return NULL;
  792. return cfg80211_find_ie(ie, ies->data, ies->len);
  793. }
  794. EXPORT_SYMBOL(ieee80211_bss_get_ie);
  795. void cfg80211_upload_connect_keys(struct wireless_dev *wdev)
  796. {
  797. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  798. struct net_device *dev = wdev->netdev;
  799. int i;
  800. if (!wdev->connect_keys)
  801. return;
  802. for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++) {
  803. if (!wdev->connect_keys->params[i].cipher)
  804. continue;
  805. if (rdev_add_key(rdev, dev, i, false, NULL,
  806. &wdev->connect_keys->params[i])) {
  807. netdev_err(dev, "failed to set key %d\n", i);
  808. continue;
  809. }
  810. if (wdev->connect_keys->def == i &&
  811. rdev_set_default_key(rdev, dev, i, true, true)) {
  812. netdev_err(dev, "failed to set defkey %d\n", i);
  813. continue;
  814. }
  815. }
  816. kzfree(wdev->connect_keys);
  817. wdev->connect_keys = NULL;
  818. }
  819. void cfg80211_process_wdev_events(struct wireless_dev *wdev)
  820. {
  821. struct cfg80211_event *ev;
  822. unsigned long flags;
  823. spin_lock_irqsave(&wdev->event_lock, flags);
  824. while (!list_empty(&wdev->event_list)) {
  825. ev = list_first_entry(&wdev->event_list,
  826. struct cfg80211_event, list);
  827. list_del(&ev->list);
  828. spin_unlock_irqrestore(&wdev->event_lock, flags);
  829. wdev_lock(wdev);
  830. switch (ev->type) {
  831. case EVENT_CONNECT_RESULT:
  832. __cfg80211_connect_result(
  833. wdev->netdev,
  834. &ev->cr,
  835. ev->cr.status == WLAN_STATUS_SUCCESS);
  836. break;
  837. case EVENT_ROAMED:
  838. __cfg80211_roamed(wdev, &ev->rm);
  839. break;
  840. case EVENT_DISCONNECTED:
  841. __cfg80211_disconnected(wdev->netdev,
  842. ev->dc.ie, ev->dc.ie_len,
  843. ev->dc.reason,
  844. !ev->dc.locally_generated);
  845. break;
  846. case EVENT_IBSS_JOINED:
  847. __cfg80211_ibss_joined(wdev->netdev, ev->ij.bssid,
  848. ev->ij.channel);
  849. break;
  850. case EVENT_STOPPED:
  851. __cfg80211_leave(wiphy_to_rdev(wdev->wiphy), wdev);
  852. break;
  853. }
  854. wdev_unlock(wdev);
  855. kfree(ev);
  856. spin_lock_irqsave(&wdev->event_lock, flags);
  857. }
  858. spin_unlock_irqrestore(&wdev->event_lock, flags);
  859. }
  860. void cfg80211_process_rdev_events(struct cfg80211_registered_device *rdev)
  861. {
  862. struct wireless_dev *wdev;
  863. ASSERT_RTNL();
  864. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
  865. cfg80211_process_wdev_events(wdev);
  866. }
  867. int cfg80211_change_iface(struct cfg80211_registered_device *rdev,
  868. struct net_device *dev, enum nl80211_iftype ntype,
  869. struct vif_params *params)
  870. {
  871. int err;
  872. enum nl80211_iftype otype = dev->ieee80211_ptr->iftype;
  873. ASSERT_RTNL();
  874. /* don't support changing VLANs, you just re-create them */
  875. if (otype == NL80211_IFTYPE_AP_VLAN)
  876. return -EOPNOTSUPP;
  877. /* cannot change into P2P device or NAN */
  878. if (ntype == NL80211_IFTYPE_P2P_DEVICE ||
  879. ntype == NL80211_IFTYPE_NAN)
  880. return -EOPNOTSUPP;
  881. if (!rdev->ops->change_virtual_intf ||
  882. !(rdev->wiphy.interface_modes & (1 << ntype)))
  883. return -EOPNOTSUPP;
  884. /* if it's part of a bridge, reject changing type to station/ibss */
  885. if ((dev->priv_flags & IFF_BRIDGE_PORT) &&
  886. (ntype == NL80211_IFTYPE_ADHOC ||
  887. ntype == NL80211_IFTYPE_STATION ||
  888. ntype == NL80211_IFTYPE_P2P_CLIENT))
  889. return -EBUSY;
  890. if (ntype != otype) {
  891. dev->ieee80211_ptr->use_4addr = false;
  892. dev->ieee80211_ptr->mesh_id_up_len = 0;
  893. wdev_lock(dev->ieee80211_ptr);
  894. rdev_set_qos_map(rdev, dev, NULL);
  895. wdev_unlock(dev->ieee80211_ptr);
  896. switch (otype) {
  897. case NL80211_IFTYPE_AP:
  898. cfg80211_stop_ap(rdev, dev, true);
  899. break;
  900. case NL80211_IFTYPE_ADHOC:
  901. cfg80211_leave_ibss(rdev, dev, false);
  902. break;
  903. case NL80211_IFTYPE_STATION:
  904. case NL80211_IFTYPE_P2P_CLIENT:
  905. wdev_lock(dev->ieee80211_ptr);
  906. cfg80211_disconnect(rdev, dev,
  907. WLAN_REASON_DEAUTH_LEAVING, true);
  908. wdev_unlock(dev->ieee80211_ptr);
  909. break;
  910. case NL80211_IFTYPE_MESH_POINT:
  911. /* mesh should be handled? */
  912. break;
  913. case NL80211_IFTYPE_OCB:
  914. cfg80211_leave_ocb(rdev, dev);
  915. break;
  916. default:
  917. break;
  918. }
  919. cfg80211_process_rdev_events(rdev);
  920. cfg80211_mlme_purge_registrations(dev->ieee80211_ptr);
  921. }
  922. err = rdev_change_virtual_intf(rdev, dev, ntype, params);
  923. WARN_ON(!err && dev->ieee80211_ptr->iftype != ntype);
  924. if (!err && params && params->use_4addr != -1)
  925. dev->ieee80211_ptr->use_4addr = params->use_4addr;
  926. if (!err) {
  927. dev->priv_flags &= ~IFF_DONT_BRIDGE;
  928. switch (ntype) {
  929. case NL80211_IFTYPE_STATION:
  930. if (dev->ieee80211_ptr->use_4addr)
  931. break;
  932. /* fall through */
  933. case NL80211_IFTYPE_OCB:
  934. case NL80211_IFTYPE_P2P_CLIENT:
  935. case NL80211_IFTYPE_ADHOC:
  936. dev->priv_flags |= IFF_DONT_BRIDGE;
  937. break;
  938. case NL80211_IFTYPE_P2P_GO:
  939. case NL80211_IFTYPE_AP:
  940. case NL80211_IFTYPE_AP_VLAN:
  941. case NL80211_IFTYPE_WDS:
  942. case NL80211_IFTYPE_MESH_POINT:
  943. /* bridging OK */
  944. break;
  945. case NL80211_IFTYPE_MONITOR:
  946. /* monitor can't bridge anyway */
  947. break;
  948. case NL80211_IFTYPE_UNSPECIFIED:
  949. case NUM_NL80211_IFTYPES:
  950. /* not happening */
  951. break;
  952. case NL80211_IFTYPE_P2P_DEVICE:
  953. case NL80211_IFTYPE_NAN:
  954. WARN_ON(1);
  955. break;
  956. }
  957. }
  958. if (!err && ntype != otype && netif_running(dev)) {
  959. cfg80211_update_iface_num(rdev, ntype, 1);
  960. cfg80211_update_iface_num(rdev, otype, -1);
  961. }
  962. return err;
  963. }
  964. static u32 cfg80211_calculate_bitrate_ht(struct rate_info *rate)
  965. {
  966. int modulation, streams, bitrate;
  967. /* the formula below does only work for MCS values smaller than 32 */
  968. if (WARN_ON_ONCE(rate->mcs >= 32))
  969. return 0;
  970. modulation = rate->mcs & 7;
  971. streams = (rate->mcs >> 3) + 1;
  972. bitrate = (rate->bw == RATE_INFO_BW_40) ? 13500000 : 6500000;
  973. if (modulation < 4)
  974. bitrate *= (modulation + 1);
  975. else if (modulation == 4)
  976. bitrate *= (modulation + 2);
  977. else
  978. bitrate *= (modulation + 3);
  979. bitrate *= streams;
  980. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  981. bitrate = (bitrate / 9) * 10;
  982. /* do NOT round down here */
  983. return (bitrate + 50000) / 100000;
  984. }
  985. static u32 cfg80211_calculate_bitrate_60g(struct rate_info *rate)
  986. {
  987. static const u32 __mcs2bitrate[] = {
  988. /* control PHY */
  989. [0] = 275,
  990. /* SC PHY */
  991. [1] = 3850,
  992. [2] = 7700,
  993. [3] = 9625,
  994. [4] = 11550,
  995. [5] = 12512, /* 1251.25 mbps */
  996. [6] = 15400,
  997. [7] = 19250,
  998. [8] = 23100,
  999. [9] = 25025,
  1000. [10] = 30800,
  1001. [11] = 38500,
  1002. [12] = 46200,
  1003. /* OFDM PHY */
  1004. [13] = 6930,
  1005. [14] = 8662, /* 866.25 mbps */
  1006. [15] = 13860,
  1007. [16] = 17325,
  1008. [17] = 20790,
  1009. [18] = 27720,
  1010. [19] = 34650,
  1011. [20] = 41580,
  1012. [21] = 45045,
  1013. [22] = 51975,
  1014. [23] = 62370,
  1015. [24] = 67568, /* 6756.75 mbps */
  1016. /* LP-SC PHY */
  1017. [25] = 6260,
  1018. [26] = 8340,
  1019. [27] = 11120,
  1020. [28] = 12510,
  1021. [29] = 16680,
  1022. [30] = 22240,
  1023. [31] = 25030,
  1024. };
  1025. if (WARN_ON_ONCE(rate->mcs >= ARRAY_SIZE(__mcs2bitrate)))
  1026. return 0;
  1027. return __mcs2bitrate[rate->mcs];
  1028. }
  1029. static u32 cfg80211_calculate_bitrate_vht(struct rate_info *rate)
  1030. {
  1031. static const u32 base[4][10] = {
  1032. { 6500000,
  1033. 13000000,
  1034. 19500000,
  1035. 26000000,
  1036. 39000000,
  1037. 52000000,
  1038. 58500000,
  1039. 65000000,
  1040. 78000000,
  1041. /* not in the spec, but some devices use this: */
  1042. 86500000,
  1043. },
  1044. { 13500000,
  1045. 27000000,
  1046. 40500000,
  1047. 54000000,
  1048. 81000000,
  1049. 108000000,
  1050. 121500000,
  1051. 135000000,
  1052. 162000000,
  1053. 180000000,
  1054. },
  1055. { 29300000,
  1056. 58500000,
  1057. 87800000,
  1058. 117000000,
  1059. 175500000,
  1060. 234000000,
  1061. 263300000,
  1062. 292500000,
  1063. 351000000,
  1064. 390000000,
  1065. },
  1066. { 58500000,
  1067. 117000000,
  1068. 175500000,
  1069. 234000000,
  1070. 351000000,
  1071. 468000000,
  1072. 526500000,
  1073. 585000000,
  1074. 702000000,
  1075. 780000000,
  1076. },
  1077. };
  1078. u32 bitrate;
  1079. int idx;
  1080. if (rate->mcs > 9)
  1081. goto warn;
  1082. switch (rate->bw) {
  1083. case RATE_INFO_BW_160:
  1084. idx = 3;
  1085. break;
  1086. case RATE_INFO_BW_80:
  1087. idx = 2;
  1088. break;
  1089. case RATE_INFO_BW_40:
  1090. idx = 1;
  1091. break;
  1092. case RATE_INFO_BW_5:
  1093. case RATE_INFO_BW_10:
  1094. default:
  1095. goto warn;
  1096. case RATE_INFO_BW_20:
  1097. idx = 0;
  1098. }
  1099. bitrate = base[idx][rate->mcs];
  1100. bitrate *= rate->nss;
  1101. if (rate->flags & RATE_INFO_FLAGS_SHORT_GI)
  1102. bitrate = (bitrate / 9) * 10;
  1103. /* do NOT round down here */
  1104. return (bitrate + 50000) / 100000;
  1105. warn:
  1106. WARN_ONCE(1, "invalid rate bw=%d, mcs=%d, nss=%d\n",
  1107. rate->bw, rate->mcs, rate->nss);
  1108. return 0;
  1109. }
  1110. u32 cfg80211_calculate_bitrate(struct rate_info *rate)
  1111. {
  1112. if (rate->flags & RATE_INFO_FLAGS_MCS)
  1113. return cfg80211_calculate_bitrate_ht(rate);
  1114. if (rate->flags & RATE_INFO_FLAGS_60G)
  1115. return cfg80211_calculate_bitrate_60g(rate);
  1116. if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
  1117. return cfg80211_calculate_bitrate_vht(rate);
  1118. return rate->legacy;
  1119. }
  1120. EXPORT_SYMBOL(cfg80211_calculate_bitrate);
  1121. int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
  1122. enum ieee80211_p2p_attr_id attr,
  1123. u8 *buf, unsigned int bufsize)
  1124. {
  1125. u8 *out = buf;
  1126. u16 attr_remaining = 0;
  1127. bool desired_attr = false;
  1128. u16 desired_len = 0;
  1129. while (len > 0) {
  1130. unsigned int iedatalen;
  1131. unsigned int copy;
  1132. const u8 *iedata;
  1133. if (len < 2)
  1134. return -EILSEQ;
  1135. iedatalen = ies[1];
  1136. if (iedatalen + 2 > len)
  1137. return -EILSEQ;
  1138. if (ies[0] != WLAN_EID_VENDOR_SPECIFIC)
  1139. goto cont;
  1140. if (iedatalen < 4)
  1141. goto cont;
  1142. iedata = ies + 2;
  1143. /* check WFA OUI, P2P subtype */
  1144. if (iedata[0] != 0x50 || iedata[1] != 0x6f ||
  1145. iedata[2] != 0x9a || iedata[3] != 0x09)
  1146. goto cont;
  1147. iedatalen -= 4;
  1148. iedata += 4;
  1149. /* check attribute continuation into this IE */
  1150. copy = min_t(unsigned int, attr_remaining, iedatalen);
  1151. if (copy && desired_attr) {
  1152. desired_len += copy;
  1153. if (out) {
  1154. memcpy(out, iedata, min(bufsize, copy));
  1155. out += min(bufsize, copy);
  1156. bufsize -= min(bufsize, copy);
  1157. }
  1158. if (copy == attr_remaining)
  1159. return desired_len;
  1160. }
  1161. attr_remaining -= copy;
  1162. if (attr_remaining)
  1163. goto cont;
  1164. iedatalen -= copy;
  1165. iedata += copy;
  1166. while (iedatalen > 0) {
  1167. u16 attr_len;
  1168. /* P2P attribute ID & size must fit */
  1169. if (iedatalen < 3)
  1170. return -EILSEQ;
  1171. desired_attr = iedata[0] == attr;
  1172. attr_len = get_unaligned_le16(iedata + 1);
  1173. iedatalen -= 3;
  1174. iedata += 3;
  1175. copy = min_t(unsigned int, attr_len, iedatalen);
  1176. if (desired_attr) {
  1177. desired_len += copy;
  1178. if (out) {
  1179. memcpy(out, iedata, min(bufsize, copy));
  1180. out += min(bufsize, copy);
  1181. bufsize -= min(bufsize, copy);
  1182. }
  1183. if (copy == attr_len)
  1184. return desired_len;
  1185. }
  1186. iedata += copy;
  1187. iedatalen -= copy;
  1188. attr_remaining = attr_len - copy;
  1189. }
  1190. cont:
  1191. len -= ies[1] + 2;
  1192. ies += ies[1] + 2;
  1193. }
  1194. if (attr_remaining && desired_attr)
  1195. return -EILSEQ;
  1196. return -ENOENT;
  1197. }
  1198. EXPORT_SYMBOL(cfg80211_get_p2p_attr);
  1199. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1200. {
  1201. int i;
  1202. for (i = 0; i < n_ids; i++)
  1203. if (ids[i] == id)
  1204. return true;
  1205. return false;
  1206. }
  1207. static size_t skip_ie(const u8 *ies, size_t ielen, size_t pos)
  1208. {
  1209. /* we assume a validly formed IEs buffer */
  1210. u8 len = ies[pos + 1];
  1211. pos += 2 + len;
  1212. /* the IE itself must have 255 bytes for fragments to follow */
  1213. if (len < 255)
  1214. return pos;
  1215. while (pos < ielen && ies[pos] == WLAN_EID_FRAGMENT) {
  1216. len = ies[pos + 1];
  1217. pos += 2 + len;
  1218. }
  1219. return pos;
  1220. }
  1221. size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
  1222. const u8 *ids, int n_ids,
  1223. const u8 *after_ric, int n_after_ric,
  1224. size_t offset)
  1225. {
  1226. size_t pos = offset;
  1227. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
  1228. if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
  1229. pos = skip_ie(ies, ielen, pos);
  1230. while (pos < ielen &&
  1231. !ieee80211_id_in_list(after_ric, n_after_ric,
  1232. ies[pos]))
  1233. pos = skip_ie(ies, ielen, pos);
  1234. } else {
  1235. pos = skip_ie(ies, ielen, pos);
  1236. }
  1237. }
  1238. return pos;
  1239. }
  1240. EXPORT_SYMBOL(ieee80211_ie_split_ric);
  1241. bool ieee80211_operating_class_to_band(u8 operating_class,
  1242. enum nl80211_band *band)
  1243. {
  1244. switch (operating_class) {
  1245. case 112:
  1246. case 115 ... 127:
  1247. case 128 ... 130:
  1248. *band = NL80211_BAND_5GHZ;
  1249. return true;
  1250. case 81:
  1251. case 82:
  1252. case 83:
  1253. case 84:
  1254. *band = NL80211_BAND_2GHZ;
  1255. return true;
  1256. case 180:
  1257. *band = NL80211_BAND_60GHZ;
  1258. return true;
  1259. }
  1260. return false;
  1261. }
  1262. EXPORT_SYMBOL(ieee80211_operating_class_to_band);
  1263. bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
  1264. u8 *op_class)
  1265. {
  1266. u8 vht_opclass;
  1267. u32 freq = chandef->center_freq1;
  1268. if (freq >= 2412 && freq <= 2472) {
  1269. if (chandef->width > NL80211_CHAN_WIDTH_40)
  1270. return false;
  1271. /* 2.407 GHz, channels 1..13 */
  1272. if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1273. if (freq > chandef->chan->center_freq)
  1274. *op_class = 83; /* HT40+ */
  1275. else
  1276. *op_class = 84; /* HT40- */
  1277. } else {
  1278. *op_class = 81;
  1279. }
  1280. return true;
  1281. }
  1282. if (freq == 2484) {
  1283. if (chandef->width > NL80211_CHAN_WIDTH_40)
  1284. return false;
  1285. *op_class = 82; /* channel 14 */
  1286. return true;
  1287. }
  1288. switch (chandef->width) {
  1289. case NL80211_CHAN_WIDTH_80:
  1290. vht_opclass = 128;
  1291. break;
  1292. case NL80211_CHAN_WIDTH_160:
  1293. vht_opclass = 129;
  1294. break;
  1295. case NL80211_CHAN_WIDTH_80P80:
  1296. vht_opclass = 130;
  1297. break;
  1298. case NL80211_CHAN_WIDTH_10:
  1299. case NL80211_CHAN_WIDTH_5:
  1300. return false; /* unsupported for now */
  1301. default:
  1302. vht_opclass = 0;
  1303. break;
  1304. }
  1305. /* 5 GHz, channels 36..48 */
  1306. if (freq >= 5180 && freq <= 5240) {
  1307. if (vht_opclass) {
  1308. *op_class = vht_opclass;
  1309. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1310. if (freq > chandef->chan->center_freq)
  1311. *op_class = 116;
  1312. else
  1313. *op_class = 117;
  1314. } else {
  1315. *op_class = 115;
  1316. }
  1317. return true;
  1318. }
  1319. /* 5 GHz, channels 52..64 */
  1320. if (freq >= 5260 && freq <= 5320) {
  1321. if (vht_opclass) {
  1322. *op_class = vht_opclass;
  1323. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1324. if (freq > chandef->chan->center_freq)
  1325. *op_class = 119;
  1326. else
  1327. *op_class = 120;
  1328. } else {
  1329. *op_class = 118;
  1330. }
  1331. return true;
  1332. }
  1333. /* 5 GHz, channels 100..144 */
  1334. if (freq >= 5500 && freq <= 5720) {
  1335. if (vht_opclass) {
  1336. *op_class = vht_opclass;
  1337. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1338. if (freq > chandef->chan->center_freq)
  1339. *op_class = 122;
  1340. else
  1341. *op_class = 123;
  1342. } else {
  1343. *op_class = 121;
  1344. }
  1345. return true;
  1346. }
  1347. /* 5 GHz, channels 149..169 */
  1348. if (freq >= 5745 && freq <= 5845) {
  1349. if (vht_opclass) {
  1350. *op_class = vht_opclass;
  1351. } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
  1352. if (freq > chandef->chan->center_freq)
  1353. *op_class = 126;
  1354. else
  1355. *op_class = 127;
  1356. } else if (freq <= 5805) {
  1357. *op_class = 124;
  1358. } else {
  1359. *op_class = 125;
  1360. }
  1361. return true;
  1362. }
  1363. /* 56.16 GHz, channel 1..4 */
  1364. if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
  1365. if (chandef->width >= NL80211_CHAN_WIDTH_40)
  1366. return false;
  1367. *op_class = 180;
  1368. return true;
  1369. }
  1370. /* not supported yet */
  1371. return false;
  1372. }
  1373. EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
  1374. static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,
  1375. u32 *beacon_int_gcd,
  1376. bool *beacon_int_different)
  1377. {
  1378. struct wireless_dev *wdev;
  1379. *beacon_int_gcd = 0;
  1380. *beacon_int_different = false;
  1381. list_for_each_entry(wdev, &wiphy->wdev_list, list) {
  1382. if (!wdev->beacon_interval)
  1383. continue;
  1384. if (!*beacon_int_gcd) {
  1385. *beacon_int_gcd = wdev->beacon_interval;
  1386. continue;
  1387. }
  1388. if (wdev->beacon_interval == *beacon_int_gcd)
  1389. continue;
  1390. *beacon_int_different = true;
  1391. *beacon_int_gcd = gcd(*beacon_int_gcd, wdev->beacon_interval);
  1392. }
  1393. if (new_beacon_int && *beacon_int_gcd != new_beacon_int) {
  1394. if (*beacon_int_gcd)
  1395. *beacon_int_different = true;
  1396. *beacon_int_gcd = gcd(*beacon_int_gcd, new_beacon_int);
  1397. }
  1398. }
  1399. int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
  1400. enum nl80211_iftype iftype, u32 beacon_int)
  1401. {
  1402. /*
  1403. * This is just a basic pre-condition check; if interface combinations
  1404. * are possible the driver must already be checking those with a call
  1405. * to cfg80211_check_combinations(), in which case we'll validate more
  1406. * through the cfg80211_calculate_bi_data() call and code in
  1407. * cfg80211_iter_combinations().
  1408. */
  1409. if (beacon_int < 10 || beacon_int > 10000)
  1410. return -EINVAL;
  1411. return 0;
  1412. }
  1413. int cfg80211_iter_combinations(struct wiphy *wiphy,
  1414. struct iface_combination_params *params,
  1415. void (*iter)(const struct ieee80211_iface_combination *c,
  1416. void *data),
  1417. void *data)
  1418. {
  1419. const struct ieee80211_regdomain *regdom;
  1420. enum nl80211_dfs_regions region = 0;
  1421. int i, j, iftype;
  1422. int num_interfaces = 0;
  1423. u32 used_iftypes = 0;
  1424. u32 beacon_int_gcd;
  1425. bool beacon_int_different;
  1426. /*
  1427. * This is a bit strange, since the iteration used to rely only on
  1428. * the data given by the driver, but here it now relies on context,
  1429. * in form of the currently operating interfaces.
  1430. * This is OK for all current users, and saves us from having to
  1431. * push the GCD calculations into all the drivers.
  1432. * In the future, this should probably rely more on data that's in
  1433. * cfg80211 already - the only thing not would appear to be any new
  1434. * interfaces (while being brought up) and channel/radar data.
  1435. */
  1436. cfg80211_calculate_bi_data(wiphy, params->new_beacon_int,
  1437. &beacon_int_gcd, &beacon_int_different);
  1438. if (params->radar_detect) {
  1439. rcu_read_lock();
  1440. regdom = rcu_dereference(cfg80211_regdomain);
  1441. if (regdom)
  1442. region = regdom->dfs_region;
  1443. rcu_read_unlock();
  1444. }
  1445. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  1446. num_interfaces += params->iftype_num[iftype];
  1447. if (params->iftype_num[iftype] > 0 &&
  1448. !(wiphy->software_iftypes & BIT(iftype)))
  1449. used_iftypes |= BIT(iftype);
  1450. }
  1451. for (i = 0; i < wiphy->n_iface_combinations; i++) {
  1452. const struct ieee80211_iface_combination *c;
  1453. struct ieee80211_iface_limit *limits;
  1454. u32 all_iftypes = 0;
  1455. c = &wiphy->iface_combinations[i];
  1456. if (num_interfaces > c->max_interfaces)
  1457. continue;
  1458. if (params->num_different_channels > c->num_different_channels)
  1459. continue;
  1460. limits = kmemdup(c->limits, sizeof(limits[0]) * c->n_limits,
  1461. GFP_KERNEL);
  1462. if (!limits)
  1463. return -ENOMEM;
  1464. for (iftype = 0; iftype < NUM_NL80211_IFTYPES; iftype++) {
  1465. if (wiphy->software_iftypes & BIT(iftype))
  1466. continue;
  1467. for (j = 0; j < c->n_limits; j++) {
  1468. all_iftypes |= limits[j].types;
  1469. if (!(limits[j].types & BIT(iftype)))
  1470. continue;
  1471. if (limits[j].max < params->iftype_num[iftype])
  1472. goto cont;
  1473. limits[j].max -= params->iftype_num[iftype];
  1474. }
  1475. }
  1476. if (params->radar_detect !=
  1477. (c->radar_detect_widths & params->radar_detect))
  1478. goto cont;
  1479. if (params->radar_detect && c->radar_detect_regions &&
  1480. !(c->radar_detect_regions & BIT(region)))
  1481. goto cont;
  1482. /* Finally check that all iftypes that we're currently
  1483. * using are actually part of this combination. If they
  1484. * aren't then we can't use this combination and have
  1485. * to continue to the next.
  1486. */
  1487. if ((all_iftypes & used_iftypes) != used_iftypes)
  1488. goto cont;
  1489. if (beacon_int_gcd) {
  1490. if (c->beacon_int_min_gcd &&
  1491. beacon_int_gcd < c->beacon_int_min_gcd)
  1492. goto cont;
  1493. if (!c->beacon_int_min_gcd && beacon_int_different)
  1494. goto cont;
  1495. }
  1496. /* This combination covered all interface types and
  1497. * supported the requested numbers, so we're good.
  1498. */
  1499. (*iter)(c, data);
  1500. cont:
  1501. kfree(limits);
  1502. }
  1503. return 0;
  1504. }
  1505. EXPORT_SYMBOL(cfg80211_iter_combinations);
  1506. static void
  1507. cfg80211_iter_sum_ifcombs(const struct ieee80211_iface_combination *c,
  1508. void *data)
  1509. {
  1510. int *num = data;
  1511. (*num)++;
  1512. }
  1513. int cfg80211_check_combinations(struct wiphy *wiphy,
  1514. struct iface_combination_params *params)
  1515. {
  1516. int err, num = 0;
  1517. err = cfg80211_iter_combinations(wiphy, params,
  1518. cfg80211_iter_sum_ifcombs, &num);
  1519. if (err)
  1520. return err;
  1521. if (num == 0)
  1522. return -EBUSY;
  1523. return 0;
  1524. }
  1525. EXPORT_SYMBOL(cfg80211_check_combinations);
  1526. int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
  1527. const u8 *rates, unsigned int n_rates,
  1528. u32 *mask)
  1529. {
  1530. int i, j;
  1531. if (!sband)
  1532. return -EINVAL;
  1533. if (n_rates == 0 || n_rates > NL80211_MAX_SUPP_RATES)
  1534. return -EINVAL;
  1535. *mask = 0;
  1536. for (i = 0; i < n_rates; i++) {
  1537. int rate = (rates[i] & 0x7f) * 5;
  1538. bool found = false;
  1539. for (j = 0; j < sband->n_bitrates; j++) {
  1540. if (sband->bitrates[j].bitrate == rate) {
  1541. found = true;
  1542. *mask |= BIT(j);
  1543. break;
  1544. }
  1545. }
  1546. if (!found)
  1547. return -EINVAL;
  1548. }
  1549. /*
  1550. * mask must have at least one bit set here since we
  1551. * didn't accept a 0-length rates array nor allowed
  1552. * entries in the array that didn't exist
  1553. */
  1554. return 0;
  1555. }
  1556. unsigned int ieee80211_get_num_supported_channels(struct wiphy *wiphy)
  1557. {
  1558. enum nl80211_band band;
  1559. unsigned int n_channels = 0;
  1560. for (band = 0; band < NUM_NL80211_BANDS; band++)
  1561. if (wiphy->bands[band])
  1562. n_channels += wiphy->bands[band]->n_channels;
  1563. return n_channels;
  1564. }
  1565. EXPORT_SYMBOL(ieee80211_get_num_supported_channels);
  1566. int cfg80211_get_station(struct net_device *dev, const u8 *mac_addr,
  1567. struct station_info *sinfo)
  1568. {
  1569. struct cfg80211_registered_device *rdev;
  1570. struct wireless_dev *wdev;
  1571. wdev = dev->ieee80211_ptr;
  1572. if (!wdev)
  1573. return -EOPNOTSUPP;
  1574. rdev = wiphy_to_rdev(wdev->wiphy);
  1575. if (!rdev->ops->get_station)
  1576. return -EOPNOTSUPP;
  1577. return rdev_get_station(rdev, dev, mac_addr, sinfo);
  1578. }
  1579. EXPORT_SYMBOL(cfg80211_get_station);
  1580. void cfg80211_free_nan_func(struct cfg80211_nan_func *f)
  1581. {
  1582. int i;
  1583. if (!f)
  1584. return;
  1585. kfree(f->serv_spec_info);
  1586. kfree(f->srf_bf);
  1587. kfree(f->srf_macs);
  1588. for (i = 0; i < f->num_rx_filters; i++)
  1589. kfree(f->rx_filters[i].filter);
  1590. for (i = 0; i < f->num_tx_filters; i++)
  1591. kfree(f->tx_filters[i].filter);
  1592. kfree(f->rx_filters);
  1593. kfree(f->tx_filters);
  1594. kfree(f);
  1595. }
  1596. EXPORT_SYMBOL(cfg80211_free_nan_func);
  1597. bool cfg80211_does_bw_fit_range(const struct ieee80211_freq_range *freq_range,
  1598. u32 center_freq_khz, u32 bw_khz)
  1599. {
  1600. u32 start_freq_khz, end_freq_khz;
  1601. start_freq_khz = center_freq_khz - (bw_khz / 2);
  1602. end_freq_khz = center_freq_khz + (bw_khz / 2);
  1603. if (start_freq_khz >= freq_range->start_freq_khz &&
  1604. end_freq_khz <= freq_range->end_freq_khz)
  1605. return true;
  1606. return false;
  1607. }
  1608. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  1609. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  1610. const unsigned char rfc1042_header[] __aligned(2) =
  1611. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  1612. EXPORT_SYMBOL(rfc1042_header);
  1613. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  1614. const unsigned char bridge_tunnel_header[] __aligned(2) =
  1615. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  1616. EXPORT_SYMBOL(bridge_tunnel_header);
  1617. /* Layer 2 Update frame (802.2 Type 1 LLC XID Update response) */
  1618. struct iapp_layer2_update {
  1619. u8 da[ETH_ALEN]; /* broadcast */
  1620. u8 sa[ETH_ALEN]; /* STA addr */
  1621. __be16 len; /* 6 */
  1622. u8 dsap; /* 0 */
  1623. u8 ssap; /* 0 */
  1624. u8 control;
  1625. u8 xid_info[3];
  1626. } __packed;
  1627. void cfg80211_send_layer2_update(struct net_device *dev, const u8 *addr)
  1628. {
  1629. struct iapp_layer2_update *msg;
  1630. struct sk_buff *skb;
  1631. /* Send Level 2 Update Frame to update forwarding tables in layer 2
  1632. * bridge devices */
  1633. skb = dev_alloc_skb(sizeof(*msg));
  1634. if (!skb)
  1635. return;
  1636. msg = skb_put(skb, sizeof(*msg));
  1637. /* 802.2 Type 1 Logical Link Control (LLC) Exchange Identifier (XID)
  1638. * Update response frame; IEEE Std 802.2-1998, 5.4.1.2.1 */
  1639. eth_broadcast_addr(msg->da);
  1640. ether_addr_copy(msg->sa, addr);
  1641. msg->len = htons(6);
  1642. msg->dsap = 0;
  1643. msg->ssap = 0x01; /* NULL LSAP, CR Bit: Response */
  1644. msg->control = 0xaf; /* XID response lsb.1111F101.
  1645. * F=0 (no poll command; unsolicited frame) */
  1646. msg->xid_info[0] = 0x81; /* XID format identifier */
  1647. msg->xid_info[1] = 1; /* LLC types/classes: Type 1 LLC */
  1648. msg->xid_info[2] = 0; /* XID sender's receive window size (RW) */
  1649. skb->dev = dev;
  1650. skb->protocol = eth_type_trans(skb, dev);
  1651. memset(skb->cb, 0, sizeof(skb->cb));
  1652. netif_rx_ni(skb);
  1653. }
  1654. EXPORT_SYMBOL(cfg80211_send_layer2_update);