ieee80211.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <net/mac80211.h>
  11. #include <net/ieee80211_radiotap.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/wireless.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/bitmap.h>
  23. #include <net/cfg80211.h>
  24. #include "ieee80211_common.h"
  25. #include "ieee80211_i.h"
  26. #include "ieee80211_rate.h"
  27. #include "wep.h"
  28. #include "wme.h"
  29. #include "aes_ccm.h"
  30. #include "ieee80211_led.h"
  31. #include "ieee80211_cfg.h"
  32. #include "debugfs.h"
  33. #include "debugfs_netdev.h"
  34. #include "debugfs_key.h"
  35. /* privid for wiphys to determine whether they belong to us or not */
  36. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  37. /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
  38. /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
  39. const unsigned char rfc1042_header[] =
  40. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  41. /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
  42. const unsigned char bridge_tunnel_header[] =
  43. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
  44. /* No encapsulation header if EtherType < 0x600 (=length) */
  45. static const unsigned char eapol_header[] =
  46. { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
  47. /*
  48. * For seeing transmitted packets on monitor interfaces
  49. * we have a radiotap header too.
  50. */
  51. struct ieee80211_tx_status_rtap_hdr {
  52. struct ieee80211_radiotap_header hdr;
  53. __le16 tx_flags;
  54. u8 data_retries;
  55. } __attribute__ ((packed));
  56. struct ieee80211_key_conf *
  57. ieee80211_key_data2conf(struct ieee80211_local *local,
  58. const struct ieee80211_key *data)
  59. {
  60. struct ieee80211_key_conf *conf;
  61. conf = kmalloc(sizeof(*conf) + data->keylen, GFP_ATOMIC);
  62. if (!conf)
  63. return NULL;
  64. conf->hw_key_idx = data->hw_key_idx;
  65. conf->alg = data->alg;
  66. conf->keylen = data->keylen;
  67. conf->flags = 0;
  68. if (data->force_sw_encrypt)
  69. conf->flags |= IEEE80211_KEY_FORCE_SW_ENCRYPT;
  70. conf->keyidx = data->keyidx;
  71. if (data->default_tx_key)
  72. conf->flags |= IEEE80211_KEY_DEFAULT_TX_KEY;
  73. if (local->default_wep_only)
  74. conf->flags |= IEEE80211_KEY_DEFAULT_WEP_ONLY;
  75. memcpy(conf->key, data->key, data->keylen);
  76. return conf;
  77. }
  78. struct ieee80211_key *ieee80211_key_alloc(struct ieee80211_sub_if_data *sdata,
  79. int idx, size_t key_len, gfp_t flags)
  80. {
  81. struct ieee80211_key *key;
  82. key = kzalloc(sizeof(struct ieee80211_key) + key_len, flags);
  83. if (!key)
  84. return NULL;
  85. kref_init(&key->kref);
  86. return key;
  87. }
  88. static void ieee80211_key_release(struct kref *kref)
  89. {
  90. struct ieee80211_key *key;
  91. key = container_of(kref, struct ieee80211_key, kref);
  92. if (key->alg == ALG_CCMP)
  93. ieee80211_aes_key_free(key->u.ccmp.tfm);
  94. ieee80211_debugfs_key_remove(key);
  95. kfree(key);
  96. }
  97. void ieee80211_key_free(struct ieee80211_key *key)
  98. {
  99. if (key)
  100. kref_put(&key->kref, ieee80211_key_release);
  101. }
  102. static int rate_list_match(const int *rate_list, int rate)
  103. {
  104. int i;
  105. if (!rate_list)
  106. return 0;
  107. for (i = 0; rate_list[i] >= 0; i++)
  108. if (rate_list[i] == rate)
  109. return 1;
  110. return 0;
  111. }
  112. void ieee80211_prepare_rates(struct ieee80211_local *local,
  113. struct ieee80211_hw_mode *mode)
  114. {
  115. int i;
  116. for (i = 0; i < mode->num_rates; i++) {
  117. struct ieee80211_rate *rate = &mode->rates[i];
  118. rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
  119. IEEE80211_RATE_BASIC);
  120. if (local->supp_rates[mode->mode]) {
  121. if (!rate_list_match(local->supp_rates[mode->mode],
  122. rate->rate))
  123. continue;
  124. }
  125. rate->flags |= IEEE80211_RATE_SUPPORTED;
  126. /* Use configured basic rate set if it is available. If not,
  127. * use defaults that are sane for most cases. */
  128. if (local->basic_rates[mode->mode]) {
  129. if (rate_list_match(local->basic_rates[mode->mode],
  130. rate->rate))
  131. rate->flags |= IEEE80211_RATE_BASIC;
  132. } else switch (mode->mode) {
  133. case MODE_IEEE80211A:
  134. if (rate->rate == 60 || rate->rate == 120 ||
  135. rate->rate == 240)
  136. rate->flags |= IEEE80211_RATE_BASIC;
  137. break;
  138. case MODE_IEEE80211B:
  139. if (rate->rate == 10 || rate->rate == 20)
  140. rate->flags |= IEEE80211_RATE_BASIC;
  141. break;
  142. case MODE_ATHEROS_TURBO:
  143. if (rate->rate == 120 || rate->rate == 240 ||
  144. rate->rate == 480)
  145. rate->flags |= IEEE80211_RATE_BASIC;
  146. break;
  147. case MODE_IEEE80211G:
  148. if (rate->rate == 10 || rate->rate == 20 ||
  149. rate->rate == 55 || rate->rate == 110)
  150. rate->flags |= IEEE80211_RATE_BASIC;
  151. break;
  152. }
  153. /* Set ERP and MANDATORY flags based on phymode */
  154. switch (mode->mode) {
  155. case MODE_IEEE80211A:
  156. if (rate->rate == 60 || rate->rate == 120 ||
  157. rate->rate == 240)
  158. rate->flags |= IEEE80211_RATE_MANDATORY;
  159. break;
  160. case MODE_IEEE80211B:
  161. if (rate->rate == 10)
  162. rate->flags |= IEEE80211_RATE_MANDATORY;
  163. break;
  164. case MODE_ATHEROS_TURBO:
  165. break;
  166. case MODE_IEEE80211G:
  167. if (rate->rate == 10 || rate->rate == 20 ||
  168. rate->rate == 55 || rate->rate == 110 ||
  169. rate->rate == 60 || rate->rate == 120 ||
  170. rate->rate == 240)
  171. rate->flags |= IEEE80211_RATE_MANDATORY;
  172. break;
  173. }
  174. if (ieee80211_is_erp_rate(mode->mode, rate->rate))
  175. rate->flags |= IEEE80211_RATE_ERP;
  176. }
  177. }
  178. void ieee80211_key_threshold_notify(struct net_device *dev,
  179. struct ieee80211_key *key,
  180. struct sta_info *sta)
  181. {
  182. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  183. struct sk_buff *skb;
  184. struct ieee80211_msg_key_notification *msg;
  185. /* if no one will get it anyway, don't even allocate it.
  186. * unlikely because this is only relevant for APs
  187. * where the device must be open... */
  188. if (unlikely(!local->apdev))
  189. return;
  190. skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
  191. sizeof(struct ieee80211_msg_key_notification));
  192. if (!skb)
  193. return;
  194. skb_reserve(skb, sizeof(struct ieee80211_frame_info));
  195. msg = (struct ieee80211_msg_key_notification *)
  196. skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
  197. msg->tx_rx_count = key->tx_rx_count;
  198. memcpy(msg->ifname, dev->name, IFNAMSIZ);
  199. if (sta)
  200. memcpy(msg->addr, sta->addr, ETH_ALEN);
  201. else
  202. memset(msg->addr, 0xff, ETH_ALEN);
  203. key->tx_rx_count = 0;
  204. ieee80211_rx_mgmt(local, skb, NULL,
  205. ieee80211_msg_key_threshold_notification);
  206. }
  207. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
  208. {
  209. u16 fc;
  210. if (len < 24)
  211. return NULL;
  212. fc = le16_to_cpu(hdr->frame_control);
  213. switch (fc & IEEE80211_FCTL_FTYPE) {
  214. case IEEE80211_FTYPE_DATA:
  215. switch (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) {
  216. case IEEE80211_FCTL_TODS:
  217. return hdr->addr1;
  218. case (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS):
  219. return NULL;
  220. case IEEE80211_FCTL_FROMDS:
  221. return hdr->addr2;
  222. case 0:
  223. return hdr->addr3;
  224. }
  225. break;
  226. case IEEE80211_FTYPE_MGMT:
  227. return hdr->addr3;
  228. case IEEE80211_FTYPE_CTL:
  229. if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PSPOLL)
  230. return hdr->addr1;
  231. else
  232. return NULL;
  233. }
  234. return NULL;
  235. }
  236. int ieee80211_get_hdrlen(u16 fc)
  237. {
  238. int hdrlen = 24;
  239. switch (fc & IEEE80211_FCTL_FTYPE) {
  240. case IEEE80211_FTYPE_DATA:
  241. if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
  242. hdrlen = 30; /* Addr4 */
  243. /*
  244. * The QoS Control field is two bytes and its presence is
  245. * indicated by the IEEE80211_STYPE_QOS_DATA bit. Add 2 to
  246. * hdrlen if that bit is set.
  247. * This works by masking out the bit and shifting it to
  248. * bit position 1 so the result has the value 0 or 2.
  249. */
  250. hdrlen += (fc & IEEE80211_STYPE_QOS_DATA)
  251. >> (ilog2(IEEE80211_STYPE_QOS_DATA)-1);
  252. break;
  253. case IEEE80211_FTYPE_CTL:
  254. /*
  255. * ACK and CTS are 10 bytes, all others 16. To see how
  256. * to get this condition consider
  257. * subtype mask: 0b0000000011110000 (0x00F0)
  258. * ACK subtype: 0b0000000011010000 (0x00D0)
  259. * CTS subtype: 0b0000000011000000 (0x00C0)
  260. * bits that matter: ^^^ (0x00E0)
  261. * value of those: 0b0000000011000000 (0x00C0)
  262. */
  263. if ((fc & 0xE0) == 0xC0)
  264. hdrlen = 10;
  265. else
  266. hdrlen = 16;
  267. break;
  268. }
  269. return hdrlen;
  270. }
  271. EXPORT_SYMBOL(ieee80211_get_hdrlen);
  272. int ieee80211_get_hdrlen_from_skb(const struct sk_buff *skb)
  273. {
  274. const struct ieee80211_hdr *hdr = (const struct ieee80211_hdr *) skb->data;
  275. int hdrlen;
  276. if (unlikely(skb->len < 10))
  277. return 0;
  278. hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
  279. if (unlikely(hdrlen > skb->len))
  280. return 0;
  281. return hdrlen;
  282. }
  283. EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
  284. int ieee80211_is_eapol(const struct sk_buff *skb)
  285. {
  286. const struct ieee80211_hdr *hdr;
  287. u16 fc;
  288. int hdrlen;
  289. if (unlikely(skb->len < 10))
  290. return 0;
  291. hdr = (const struct ieee80211_hdr *) skb->data;
  292. fc = le16_to_cpu(hdr->frame_control);
  293. if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
  294. return 0;
  295. hdrlen = ieee80211_get_hdrlen(fc);
  296. if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
  297. memcmp(skb->data + hdrlen, eapol_header,
  298. sizeof(eapol_header)) == 0))
  299. return 1;
  300. return 0;
  301. }
  302. void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
  303. {
  304. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  305. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  306. if (tx->u.tx.extra_frag) {
  307. struct ieee80211_hdr *fhdr;
  308. int i;
  309. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  310. fhdr = (struct ieee80211_hdr *)
  311. tx->u.tx.extra_frag[i]->data;
  312. fhdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  313. }
  314. }
  315. }
  316. static int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
  317. int rate, int erp, int short_preamble)
  318. {
  319. int dur;
  320. /* calculate duration (in microseconds, rounded up to next higher
  321. * integer if it includes a fractional microsecond) to send frame of
  322. * len bytes (does not include FCS) at the given rate. Duration will
  323. * also include SIFS.
  324. *
  325. * rate is in 100 kbps, so divident is multiplied by 10 in the
  326. * DIV_ROUND_UP() operations.
  327. */
  328. if (local->hw.conf.phymode == MODE_IEEE80211A || erp ||
  329. local->hw.conf.phymode == MODE_ATHEROS_TURBO) {
  330. /*
  331. * OFDM:
  332. *
  333. * N_DBPS = DATARATE x 4
  334. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  335. * (16 = SIGNAL time, 6 = tail bits)
  336. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  337. *
  338. * T_SYM = 4 usec
  339. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  340. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  341. * signal ext = 6 usec
  342. */
  343. /* FIX: Atheros Turbo may have different (shorter) duration? */
  344. dur = 16; /* SIFS + signal ext */
  345. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  346. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  347. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  348. 4 * rate); /* T_SYM x N_SYM */
  349. } else {
  350. /*
  351. * 802.11b or 802.11g with 802.11b compatibility:
  352. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  353. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  354. *
  355. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  356. * aSIFSTime = 10 usec
  357. * aPreambleLength = 144 usec or 72 usec with short preamble
  358. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  359. */
  360. dur = 10; /* aSIFSTime = 10 usec */
  361. dur += short_preamble ? (72 + 24) : (144 + 48);
  362. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  363. }
  364. return dur;
  365. }
  366. /* Exported duration function for driver use */
  367. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  368. size_t frame_len, int rate)
  369. {
  370. struct ieee80211_local *local = hw_to_local(hw);
  371. u16 dur;
  372. int erp;
  373. erp = ieee80211_is_erp_rate(hw->conf.phymode, rate);
  374. dur = ieee80211_frame_duration(local, frame_len, rate,
  375. erp, local->short_preamble);
  376. return cpu_to_le16(dur);
  377. }
  378. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  379. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  380. size_t frame_len,
  381. const struct ieee80211_tx_control *frame_txctl)
  382. {
  383. struct ieee80211_local *local = hw_to_local(hw);
  384. struct ieee80211_rate *rate;
  385. int short_preamble = local->short_preamble;
  386. int erp;
  387. u16 dur;
  388. rate = frame_txctl->rts_rate;
  389. erp = !!(rate->flags & IEEE80211_RATE_ERP);
  390. /* CTS duration */
  391. dur = ieee80211_frame_duration(local, 10, rate->rate,
  392. erp, short_preamble);
  393. /* Data frame duration */
  394. dur += ieee80211_frame_duration(local, frame_len, rate->rate,
  395. erp, short_preamble);
  396. /* ACK duration */
  397. dur += ieee80211_frame_duration(local, 10, rate->rate,
  398. erp, short_preamble);
  399. return cpu_to_le16(dur);
  400. }
  401. EXPORT_SYMBOL(ieee80211_rts_duration);
  402. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  403. size_t frame_len,
  404. const struct ieee80211_tx_control *frame_txctl)
  405. {
  406. struct ieee80211_local *local = hw_to_local(hw);
  407. struct ieee80211_rate *rate;
  408. int short_preamble = local->short_preamble;
  409. int erp;
  410. u16 dur;
  411. rate = frame_txctl->rts_rate;
  412. erp = !!(rate->flags & IEEE80211_RATE_ERP);
  413. /* Data frame duration */
  414. dur = ieee80211_frame_duration(local, frame_len, rate->rate,
  415. erp, short_preamble);
  416. if (!(frame_txctl->flags & IEEE80211_TXCTL_NO_ACK)) {
  417. /* ACK duration */
  418. dur += ieee80211_frame_duration(local, 10, rate->rate,
  419. erp, short_preamble);
  420. }
  421. return cpu_to_le16(dur);
  422. }
  423. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  424. static int __ieee80211_if_config(struct net_device *dev,
  425. struct sk_buff *beacon,
  426. struct ieee80211_tx_control *control)
  427. {
  428. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  429. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  430. struct ieee80211_if_conf conf;
  431. static u8 scan_bssid[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  432. if (!local->ops->config_interface || !netif_running(dev))
  433. return 0;
  434. memset(&conf, 0, sizeof(conf));
  435. conf.type = sdata->type;
  436. if (sdata->type == IEEE80211_IF_TYPE_STA ||
  437. sdata->type == IEEE80211_IF_TYPE_IBSS) {
  438. if (local->sta_scanning &&
  439. local->scan_dev == dev)
  440. conf.bssid = scan_bssid;
  441. else
  442. conf.bssid = sdata->u.sta.bssid;
  443. conf.ssid = sdata->u.sta.ssid;
  444. conf.ssid_len = sdata->u.sta.ssid_len;
  445. conf.generic_elem = sdata->u.sta.extra_ie;
  446. conf.generic_elem_len = sdata->u.sta.extra_ie_len;
  447. } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
  448. conf.ssid = sdata->u.ap.ssid;
  449. conf.ssid_len = sdata->u.ap.ssid_len;
  450. conf.generic_elem = sdata->u.ap.generic_elem;
  451. conf.generic_elem_len = sdata->u.ap.generic_elem_len;
  452. conf.beacon = beacon;
  453. conf.beacon_control = control;
  454. }
  455. return local->ops->config_interface(local_to_hw(local),
  456. dev->ifindex, &conf);
  457. }
  458. int ieee80211_if_config(struct net_device *dev)
  459. {
  460. return __ieee80211_if_config(dev, NULL, NULL);
  461. }
  462. int ieee80211_if_config_beacon(struct net_device *dev)
  463. {
  464. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  465. struct ieee80211_tx_control control;
  466. struct sk_buff *skb;
  467. if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
  468. return 0;
  469. skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
  470. if (!skb)
  471. return -ENOMEM;
  472. return __ieee80211_if_config(dev, skb, &control);
  473. }
  474. int ieee80211_hw_config(struct ieee80211_local *local)
  475. {
  476. struct ieee80211_hw_mode *mode;
  477. struct ieee80211_channel *chan;
  478. int ret = 0;
  479. if (local->sta_scanning) {
  480. chan = local->scan_channel;
  481. mode = local->scan_hw_mode;
  482. } else {
  483. chan = local->oper_channel;
  484. mode = local->oper_hw_mode;
  485. }
  486. local->hw.conf.channel = chan->chan;
  487. local->hw.conf.channel_val = chan->val;
  488. local->hw.conf.power_level = chan->power_level;
  489. local->hw.conf.freq = chan->freq;
  490. local->hw.conf.phymode = mode->mode;
  491. local->hw.conf.antenna_max = chan->antenna_max;
  492. local->hw.conf.chan = chan;
  493. local->hw.conf.mode = mode;
  494. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  495. printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
  496. "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
  497. local->hw.conf.phymode);
  498. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  499. if (local->ops->config)
  500. ret = local->ops->config(local_to_hw(local), &local->hw.conf);
  501. return ret;
  502. }
  503. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  504. {
  505. /* FIX: what would be proper limits for MTU?
  506. * This interface uses 802.3 frames. */
  507. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
  508. printk(KERN_WARNING "%s: invalid MTU %d\n",
  509. dev->name, new_mtu);
  510. return -EINVAL;
  511. }
  512. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  513. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  514. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  515. dev->mtu = new_mtu;
  516. return 0;
  517. }
  518. static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
  519. {
  520. /* FIX: what would be proper limits for MTU?
  521. * This interface uses 802.11 frames. */
  522. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
  523. printk(KERN_WARNING "%s: invalid MTU %d\n",
  524. dev->name, new_mtu);
  525. return -EINVAL;
  526. }
  527. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  528. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  529. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  530. dev->mtu = new_mtu;
  531. return 0;
  532. }
  533. enum netif_tx_lock_class {
  534. TX_LOCK_NORMAL,
  535. TX_LOCK_MASTER,
  536. };
  537. static inline void netif_tx_lock_nested(struct net_device *dev, int subclass)
  538. {
  539. spin_lock_nested(&dev->_xmit_lock, subclass);
  540. dev->xmit_lock_owner = smp_processor_id();
  541. }
  542. static void ieee80211_set_multicast_list(struct net_device *dev)
  543. {
  544. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  545. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  546. unsigned short flags;
  547. netif_tx_lock_nested(local->mdev, TX_LOCK_MASTER);
  548. if (((dev->flags & IFF_ALLMULTI) != 0) ^ (sdata->allmulti != 0)) {
  549. if (sdata->allmulti) {
  550. sdata->allmulti = 0;
  551. local->iff_allmultis--;
  552. } else {
  553. sdata->allmulti = 1;
  554. local->iff_allmultis++;
  555. }
  556. }
  557. if (((dev->flags & IFF_PROMISC) != 0) ^ (sdata->promisc != 0)) {
  558. if (sdata->promisc) {
  559. sdata->promisc = 0;
  560. local->iff_promiscs--;
  561. } else {
  562. sdata->promisc = 1;
  563. local->iff_promiscs++;
  564. }
  565. }
  566. if (dev->mc_count != sdata->mc_count) {
  567. local->mc_count = local->mc_count - sdata->mc_count +
  568. dev->mc_count;
  569. sdata->mc_count = dev->mc_count;
  570. }
  571. if (local->ops->set_multicast_list) {
  572. flags = local->mdev->flags;
  573. if (local->iff_allmultis)
  574. flags |= IFF_ALLMULTI;
  575. if (local->iff_promiscs)
  576. flags |= IFF_PROMISC;
  577. read_lock(&local->sub_if_lock);
  578. local->ops->set_multicast_list(local_to_hw(local), flags,
  579. local->mc_count);
  580. read_unlock(&local->sub_if_lock);
  581. }
  582. netif_tx_unlock(local->mdev);
  583. }
  584. struct dev_mc_list *ieee80211_get_mc_list_item(struct ieee80211_hw *hw,
  585. struct dev_mc_list *prev,
  586. void **ptr)
  587. {
  588. struct ieee80211_local *local = hw_to_local(hw);
  589. struct ieee80211_sub_if_data *sdata = *ptr;
  590. struct dev_mc_list *mc;
  591. if (!prev) {
  592. WARN_ON(sdata);
  593. sdata = NULL;
  594. }
  595. if (!prev || !prev->next) {
  596. if (sdata)
  597. sdata = list_entry(sdata->list.next,
  598. struct ieee80211_sub_if_data, list);
  599. else
  600. sdata = list_entry(local->sub_if_list.next,
  601. struct ieee80211_sub_if_data, list);
  602. if (&sdata->list != &local->sub_if_list)
  603. mc = sdata->dev->mc_list;
  604. else
  605. mc = NULL;
  606. } else
  607. mc = prev->next;
  608. *ptr = sdata;
  609. return mc;
  610. }
  611. EXPORT_SYMBOL(ieee80211_get_mc_list_item);
  612. static struct net_device_stats *ieee80211_get_stats(struct net_device *dev)
  613. {
  614. struct ieee80211_sub_if_data *sdata;
  615. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  616. return &(sdata->stats);
  617. }
  618. static void ieee80211_if_shutdown(struct net_device *dev)
  619. {
  620. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  621. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  622. ASSERT_RTNL();
  623. switch (sdata->type) {
  624. case IEEE80211_IF_TYPE_STA:
  625. case IEEE80211_IF_TYPE_IBSS:
  626. sdata->u.sta.state = IEEE80211_DISABLED;
  627. del_timer_sync(&sdata->u.sta.timer);
  628. skb_queue_purge(&sdata->u.sta.skb_queue);
  629. if (!local->ops->hw_scan &&
  630. local->scan_dev == sdata->dev) {
  631. local->sta_scanning = 0;
  632. cancel_delayed_work(&local->scan_work);
  633. }
  634. flush_workqueue(local->hw.workqueue);
  635. break;
  636. }
  637. }
  638. static inline int identical_mac_addr_allowed(int type1, int type2)
  639. {
  640. return (type1 == IEEE80211_IF_TYPE_MNTR ||
  641. type2 == IEEE80211_IF_TYPE_MNTR ||
  642. (type1 == IEEE80211_IF_TYPE_AP &&
  643. type2 == IEEE80211_IF_TYPE_WDS) ||
  644. (type1 == IEEE80211_IF_TYPE_WDS &&
  645. (type2 == IEEE80211_IF_TYPE_WDS ||
  646. type2 == IEEE80211_IF_TYPE_AP)) ||
  647. (type1 == IEEE80211_IF_TYPE_AP &&
  648. type2 == IEEE80211_IF_TYPE_VLAN) ||
  649. (type1 == IEEE80211_IF_TYPE_VLAN &&
  650. (type2 == IEEE80211_IF_TYPE_AP ||
  651. type2 == IEEE80211_IF_TYPE_VLAN)));
  652. }
  653. static int ieee80211_master_open(struct net_device *dev)
  654. {
  655. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  656. struct ieee80211_sub_if_data *sdata;
  657. int res = -EOPNOTSUPP;
  658. read_lock(&local->sub_if_lock);
  659. list_for_each_entry(sdata, &local->sub_if_list, list) {
  660. if (sdata->dev != dev && netif_running(sdata->dev)) {
  661. res = 0;
  662. break;
  663. }
  664. }
  665. read_unlock(&local->sub_if_lock);
  666. return res;
  667. }
  668. static int ieee80211_master_stop(struct net_device *dev)
  669. {
  670. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  671. struct ieee80211_sub_if_data *sdata;
  672. read_lock(&local->sub_if_lock);
  673. list_for_each_entry(sdata, &local->sub_if_list, list)
  674. if (sdata->dev != dev && netif_running(sdata->dev))
  675. dev_close(sdata->dev);
  676. read_unlock(&local->sub_if_lock);
  677. return 0;
  678. }
  679. static int ieee80211_mgmt_open(struct net_device *dev)
  680. {
  681. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  682. if (!netif_running(local->mdev))
  683. return -EOPNOTSUPP;
  684. return 0;
  685. }
  686. static int ieee80211_mgmt_stop(struct net_device *dev)
  687. {
  688. return 0;
  689. }
  690. /* Check if running monitor interfaces should go to a "soft monitor" mode
  691. * and switch them if necessary. */
  692. static inline void ieee80211_start_soft_monitor(struct ieee80211_local *local)
  693. {
  694. struct ieee80211_if_init_conf conf;
  695. if (local->open_count && local->open_count == local->monitors &&
  696. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER) &&
  697. local->ops->remove_interface) {
  698. conf.if_id = -1;
  699. conf.type = IEEE80211_IF_TYPE_MNTR;
  700. conf.mac_addr = NULL;
  701. local->ops->remove_interface(local_to_hw(local), &conf);
  702. }
  703. }
  704. /* Check if running monitor interfaces should go to a "hard monitor" mode
  705. * and switch them if necessary. */
  706. static void ieee80211_start_hard_monitor(struct ieee80211_local *local)
  707. {
  708. struct ieee80211_if_init_conf conf;
  709. if (local->open_count && local->open_count == local->monitors &&
  710. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  711. conf.if_id = -1;
  712. conf.type = IEEE80211_IF_TYPE_MNTR;
  713. conf.mac_addr = NULL;
  714. local->ops->add_interface(local_to_hw(local), &conf);
  715. }
  716. }
  717. static int ieee80211_open(struct net_device *dev)
  718. {
  719. struct ieee80211_sub_if_data *sdata, *nsdata;
  720. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  721. struct ieee80211_if_init_conf conf;
  722. int res;
  723. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  724. read_lock(&local->sub_if_lock);
  725. list_for_each_entry(nsdata, &local->sub_if_list, list) {
  726. struct net_device *ndev = nsdata->dev;
  727. if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
  728. compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0 &&
  729. !identical_mac_addr_allowed(sdata->type, nsdata->type)) {
  730. read_unlock(&local->sub_if_lock);
  731. return -ENOTUNIQ;
  732. }
  733. }
  734. read_unlock(&local->sub_if_lock);
  735. if (sdata->type == IEEE80211_IF_TYPE_WDS &&
  736. is_zero_ether_addr(sdata->u.wds.remote_addr))
  737. return -ENOLINK;
  738. if (sdata->type == IEEE80211_IF_TYPE_MNTR && local->open_count &&
  739. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  740. /* run the interface in a "soft monitor" mode */
  741. local->monitors++;
  742. local->open_count++;
  743. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  744. return 0;
  745. }
  746. ieee80211_start_soft_monitor(local);
  747. conf.if_id = dev->ifindex;
  748. conf.type = sdata->type;
  749. conf.mac_addr = dev->dev_addr;
  750. res = local->ops->add_interface(local_to_hw(local), &conf);
  751. if (res) {
  752. if (sdata->type == IEEE80211_IF_TYPE_MNTR)
  753. ieee80211_start_hard_monitor(local);
  754. return res;
  755. }
  756. if (local->open_count == 0) {
  757. res = 0;
  758. tasklet_enable(&local->tx_pending_tasklet);
  759. tasklet_enable(&local->tasklet);
  760. if (local->ops->open)
  761. res = local->ops->open(local_to_hw(local));
  762. if (res == 0) {
  763. res = dev_open(local->mdev);
  764. if (res) {
  765. if (local->ops->stop)
  766. local->ops->stop(local_to_hw(local));
  767. } else {
  768. res = ieee80211_hw_config(local);
  769. if (res && local->ops->stop)
  770. local->ops->stop(local_to_hw(local));
  771. else if (!res && local->apdev)
  772. dev_open(local->apdev);
  773. }
  774. }
  775. if (res) {
  776. if (local->ops->remove_interface)
  777. local->ops->remove_interface(local_to_hw(local),
  778. &conf);
  779. return res;
  780. }
  781. }
  782. local->open_count++;
  783. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  784. local->monitors++;
  785. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  786. } else
  787. ieee80211_if_config(dev);
  788. if (sdata->type == IEEE80211_IF_TYPE_STA &&
  789. !local->user_space_mlme)
  790. netif_carrier_off(dev);
  791. else
  792. netif_carrier_on(dev);
  793. netif_start_queue(dev);
  794. return 0;
  795. }
  796. static int ieee80211_stop(struct net_device *dev)
  797. {
  798. struct ieee80211_sub_if_data *sdata;
  799. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  800. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  801. if (sdata->type == IEEE80211_IF_TYPE_MNTR &&
  802. local->open_count > 1 &&
  803. !(local->hw.flags & IEEE80211_HW_MONITOR_DURING_OPER)) {
  804. /* remove "soft monitor" interface */
  805. local->open_count--;
  806. local->monitors--;
  807. if (!local->monitors)
  808. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  809. return 0;
  810. }
  811. netif_stop_queue(dev);
  812. ieee80211_if_shutdown(dev);
  813. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  814. local->monitors--;
  815. if (!local->monitors)
  816. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  817. }
  818. local->open_count--;
  819. if (local->open_count == 0) {
  820. if (netif_running(local->mdev))
  821. dev_close(local->mdev);
  822. if (local->apdev)
  823. dev_close(local->apdev);
  824. if (local->ops->stop)
  825. local->ops->stop(local_to_hw(local));
  826. tasklet_disable(&local->tx_pending_tasklet);
  827. tasklet_disable(&local->tasklet);
  828. }
  829. if (local->ops->remove_interface) {
  830. struct ieee80211_if_init_conf conf;
  831. conf.if_id = dev->ifindex;
  832. conf.type = sdata->type;
  833. conf.mac_addr = dev->dev_addr;
  834. local->ops->remove_interface(local_to_hw(local), &conf);
  835. }
  836. ieee80211_start_hard_monitor(local);
  837. return 0;
  838. }
  839. static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
  840. {
  841. memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
  842. return ETH_ALEN;
  843. }
  844. struct ieee80211_rate *
  845. ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
  846. {
  847. struct ieee80211_hw_mode *mode;
  848. int r;
  849. list_for_each_entry(mode, &local->modes_list, list) {
  850. if (mode->mode != phymode)
  851. continue;
  852. for (r = 0; r < mode->num_rates; r++) {
  853. struct ieee80211_rate *rate = &mode->rates[r];
  854. if (rate->val == hw_rate ||
  855. (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
  856. rate->val2 == hw_rate))
  857. return rate;
  858. }
  859. }
  860. return NULL;
  861. }
  862. static void
  863. ieee80211_fill_frame_info(struct ieee80211_local *local,
  864. struct ieee80211_frame_info *fi,
  865. struct ieee80211_rx_status *status)
  866. {
  867. if (status) {
  868. struct timespec ts;
  869. struct ieee80211_rate *rate;
  870. jiffies_to_timespec(jiffies, &ts);
  871. fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
  872. ts.tv_nsec / 1000);
  873. fi->mactime = cpu_to_be64(status->mactime);
  874. switch (status->phymode) {
  875. case MODE_IEEE80211A:
  876. fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
  877. break;
  878. case MODE_IEEE80211B:
  879. fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
  880. break;
  881. case MODE_IEEE80211G:
  882. fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
  883. break;
  884. case MODE_ATHEROS_TURBO:
  885. fi->phytype =
  886. htonl(ieee80211_phytype_dsss_dot11_turbo);
  887. break;
  888. default:
  889. fi->phytype = htonl(0xAAAAAAAA);
  890. break;
  891. }
  892. fi->channel = htonl(status->channel);
  893. rate = ieee80211_get_rate(local, status->phymode,
  894. status->rate);
  895. if (rate) {
  896. fi->datarate = htonl(rate->rate);
  897. if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
  898. if (status->rate == rate->val)
  899. fi->preamble = htonl(2); /* long */
  900. else if (status->rate == rate->val2)
  901. fi->preamble = htonl(1); /* short */
  902. } else
  903. fi->preamble = htonl(0);
  904. } else {
  905. fi->datarate = htonl(0);
  906. fi->preamble = htonl(0);
  907. }
  908. fi->antenna = htonl(status->antenna);
  909. fi->priority = htonl(0xffffffff); /* no clue */
  910. fi->ssi_type = htonl(ieee80211_ssi_raw);
  911. fi->ssi_signal = htonl(status->ssi);
  912. fi->ssi_noise = 0x00000000;
  913. fi->encoding = 0;
  914. } else {
  915. /* clear everything because we really don't know.
  916. * the msg_type field isn't present on monitor frames
  917. * so we don't know whether it will be present or not,
  918. * but it's ok to not clear it since it'll be assigned
  919. * anyway */
  920. memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
  921. fi->ssi_type = htonl(ieee80211_ssi_none);
  922. }
  923. fi->version = htonl(IEEE80211_FI_VERSION);
  924. fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
  925. }
  926. /* this routine is actually not just for this, but also
  927. * for pushing fake 'management' frames into userspace.
  928. * it shall be replaced by a netlink-based system. */
  929. void
  930. ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
  931. struct ieee80211_rx_status *status, u32 msg_type)
  932. {
  933. struct ieee80211_frame_info *fi;
  934. const size_t hlen = sizeof(struct ieee80211_frame_info);
  935. struct ieee80211_sub_if_data *sdata;
  936. skb->dev = local->apdev;
  937. sdata = IEEE80211_DEV_TO_SUB_IF(local->apdev);
  938. if (skb_headroom(skb) < hlen) {
  939. I802_DEBUG_INC(local->rx_expand_skb_head);
  940. if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
  941. dev_kfree_skb(skb);
  942. return;
  943. }
  944. }
  945. fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
  946. ieee80211_fill_frame_info(local, fi, status);
  947. fi->msg_type = htonl(msg_type);
  948. sdata->stats.rx_packets++;
  949. sdata->stats.rx_bytes += skb->len;
  950. skb_set_mac_header(skb, 0);
  951. skb->ip_summed = CHECKSUM_UNNECESSARY;
  952. skb->pkt_type = PACKET_OTHERHOST;
  953. skb->protocol = htons(ETH_P_802_2);
  954. memset(skb->cb, 0, sizeof(skb->cb));
  955. netif_rx(skb);
  956. }
  957. int ieee80211_radar_status(struct ieee80211_hw *hw, int channel,
  958. int radar, int radar_type)
  959. {
  960. struct sk_buff *skb;
  961. struct ieee80211_radar_info *msg;
  962. struct ieee80211_local *local = hw_to_local(hw);
  963. if (!local->apdev)
  964. return 0;
  965. skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
  966. sizeof(struct ieee80211_radar_info));
  967. if (!skb)
  968. return -ENOMEM;
  969. skb_reserve(skb, sizeof(struct ieee80211_frame_info));
  970. msg = (struct ieee80211_radar_info *)
  971. skb_put(skb, sizeof(struct ieee80211_radar_info));
  972. msg->channel = channel;
  973. msg->radar = radar;
  974. msg->radar_type = radar_type;
  975. ieee80211_rx_mgmt(local, skb, NULL, ieee80211_msg_radar);
  976. return 0;
  977. }
  978. EXPORT_SYMBOL(ieee80211_radar_status);
  979. static void ieee80211_stat_refresh(unsigned long data)
  980. {
  981. struct ieee80211_local *local = (struct ieee80211_local *) data;
  982. struct sta_info *sta;
  983. struct ieee80211_sub_if_data *sdata;
  984. if (!local->stat_time)
  985. return;
  986. /* go through all stations */
  987. spin_lock_bh(&local->sta_lock);
  988. list_for_each_entry(sta, &local->sta_list, list) {
  989. sta->channel_use = (sta->channel_use_raw / local->stat_time) /
  990. CHAN_UTIL_PER_10MS;
  991. sta->channel_use_raw = 0;
  992. }
  993. spin_unlock_bh(&local->sta_lock);
  994. /* go through all subinterfaces */
  995. read_lock(&local->sub_if_lock);
  996. list_for_each_entry(sdata, &local->sub_if_list, list) {
  997. sdata->channel_use = (sdata->channel_use_raw /
  998. local->stat_time) / CHAN_UTIL_PER_10MS;
  999. sdata->channel_use_raw = 0;
  1000. }
  1001. read_unlock(&local->sub_if_lock);
  1002. /* hardware interface */
  1003. local->channel_use = (local->channel_use_raw /
  1004. local->stat_time) / CHAN_UTIL_PER_10MS;
  1005. local->channel_use_raw = 0;
  1006. local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
  1007. add_timer(&local->stat_timer);
  1008. }
  1009. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  1010. struct sk_buff *skb,
  1011. struct ieee80211_tx_status *status)
  1012. {
  1013. struct ieee80211_local *local = hw_to_local(hw);
  1014. struct ieee80211_tx_status *saved;
  1015. int tmp;
  1016. skb->dev = local->mdev;
  1017. saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
  1018. if (unlikely(!saved)) {
  1019. if (net_ratelimit())
  1020. printk(KERN_WARNING "%s: Not enough memory, "
  1021. "dropping tx status", skb->dev->name);
  1022. /* should be dev_kfree_skb_irq, but due to this function being
  1023. * named _irqsafe instead of just _irq we can't be sure that
  1024. * people won't call it from non-irq contexts */
  1025. dev_kfree_skb_any(skb);
  1026. return;
  1027. }
  1028. memcpy(saved, status, sizeof(struct ieee80211_tx_status));
  1029. /* copy pointer to saved status into skb->cb for use by tasklet */
  1030. memcpy(skb->cb, &saved, sizeof(saved));
  1031. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  1032. skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
  1033. &local->skb_queue : &local->skb_queue_unreliable, skb);
  1034. tmp = skb_queue_len(&local->skb_queue) +
  1035. skb_queue_len(&local->skb_queue_unreliable);
  1036. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  1037. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  1038. memcpy(&saved, skb->cb, sizeof(saved));
  1039. kfree(saved);
  1040. dev_kfree_skb_irq(skb);
  1041. tmp--;
  1042. I802_DEBUG_INC(local->tx_status_drop);
  1043. }
  1044. tasklet_schedule(&local->tasklet);
  1045. }
  1046. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  1047. static void ieee80211_tasklet_handler(unsigned long data)
  1048. {
  1049. struct ieee80211_local *local = (struct ieee80211_local *) data;
  1050. struct sk_buff *skb;
  1051. struct ieee80211_rx_status rx_status;
  1052. struct ieee80211_tx_status *tx_status;
  1053. while ((skb = skb_dequeue(&local->skb_queue)) ||
  1054. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  1055. switch (skb->pkt_type) {
  1056. case IEEE80211_RX_MSG:
  1057. /* status is in skb->cb */
  1058. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  1059. /* Clear skb->type in order to not confuse kernel
  1060. * netstack. */
  1061. skb->pkt_type = 0;
  1062. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  1063. break;
  1064. case IEEE80211_TX_STATUS_MSG:
  1065. /* get pointer to saved status out of skb->cb */
  1066. memcpy(&tx_status, skb->cb, sizeof(tx_status));
  1067. skb->pkt_type = 0;
  1068. ieee80211_tx_status(local_to_hw(local),
  1069. skb, tx_status);
  1070. kfree(tx_status);
  1071. break;
  1072. default: /* should never get here! */
  1073. printk(KERN_ERR "%s: Unknown message type (%d)\n",
  1074. local->mdev->name, skb->pkt_type);
  1075. dev_kfree_skb(skb);
  1076. break;
  1077. }
  1078. }
  1079. }
  1080. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  1081. * make a prepared TX frame (one that has been given to hw) to look like brand
  1082. * new IEEE 802.11 frame that is ready to go through TX processing again.
  1083. * Also, tx_packet_data in cb is restored from tx_control. */
  1084. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  1085. struct ieee80211_key *key,
  1086. struct sk_buff *skb,
  1087. struct ieee80211_tx_control *control)
  1088. {
  1089. int hdrlen, iv_len, mic_len;
  1090. struct ieee80211_tx_packet_data *pkt_data;
  1091. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  1092. pkt_data->ifindex = control->ifindex;
  1093. pkt_data->mgmt_iface = (control->type == IEEE80211_IF_TYPE_MGMT);
  1094. pkt_data->req_tx_status = !!(control->flags & IEEE80211_TXCTL_REQ_TX_STATUS);
  1095. pkt_data->do_not_encrypt = !!(control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT);
  1096. pkt_data->requeue = !!(control->flags & IEEE80211_TXCTL_REQUEUE);
  1097. pkt_data->queue = control->queue;
  1098. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1099. if (!key)
  1100. goto no_key;
  1101. switch (key->alg) {
  1102. case ALG_WEP:
  1103. iv_len = WEP_IV_LEN;
  1104. mic_len = WEP_ICV_LEN;
  1105. break;
  1106. case ALG_TKIP:
  1107. iv_len = TKIP_IV_LEN;
  1108. mic_len = TKIP_ICV_LEN;
  1109. break;
  1110. case ALG_CCMP:
  1111. iv_len = CCMP_HDR_LEN;
  1112. mic_len = CCMP_MIC_LEN;
  1113. break;
  1114. default:
  1115. goto no_key;
  1116. }
  1117. if (skb->len >= mic_len && key->force_sw_encrypt)
  1118. skb_trim(skb, skb->len - mic_len);
  1119. if (skb->len >= iv_len && skb->len > hdrlen) {
  1120. memmove(skb->data + iv_len, skb->data, hdrlen);
  1121. skb_pull(skb, iv_len);
  1122. }
  1123. no_key:
  1124. {
  1125. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1126. u16 fc = le16_to_cpu(hdr->frame_control);
  1127. if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
  1128. fc &= ~IEEE80211_STYPE_QOS_DATA;
  1129. hdr->frame_control = cpu_to_le16(fc);
  1130. memmove(skb->data + 2, skb->data, hdrlen - 2);
  1131. skb_pull(skb, 2);
  1132. }
  1133. }
  1134. }
  1135. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  1136. struct ieee80211_tx_status *status)
  1137. {
  1138. struct sk_buff *skb2;
  1139. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1140. struct ieee80211_local *local = hw_to_local(hw);
  1141. u16 frag, type;
  1142. u32 msg_type;
  1143. struct ieee80211_tx_status_rtap_hdr *rthdr;
  1144. struct ieee80211_sub_if_data *sdata;
  1145. int monitors;
  1146. if (!status) {
  1147. printk(KERN_ERR
  1148. "%s: ieee80211_tx_status called with NULL status\n",
  1149. local->mdev->name);
  1150. dev_kfree_skb(skb);
  1151. return;
  1152. }
  1153. if (status->excessive_retries) {
  1154. struct sta_info *sta;
  1155. sta = sta_info_get(local, hdr->addr1);
  1156. if (sta) {
  1157. if (sta->flags & WLAN_STA_PS) {
  1158. /* The STA is in power save mode, so assume
  1159. * that this TX packet failed because of that.
  1160. */
  1161. status->excessive_retries = 0;
  1162. status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
  1163. }
  1164. sta_info_put(sta);
  1165. }
  1166. }
  1167. if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
  1168. struct sta_info *sta;
  1169. sta = sta_info_get(local, hdr->addr1);
  1170. if (sta) {
  1171. sta->tx_filtered_count++;
  1172. /* Clear the TX filter mask for this STA when sending
  1173. * the next packet. If the STA went to power save mode,
  1174. * this will happen when it is waking up for the next
  1175. * time. */
  1176. sta->clear_dst_mask = 1;
  1177. /* TODO: Is the WLAN_STA_PS flag always set here or is
  1178. * the race between RX and TX status causing some
  1179. * packets to be filtered out before 80211.o gets an
  1180. * update for PS status? This seems to be the case, so
  1181. * no changes are likely to be needed. */
  1182. if (sta->flags & WLAN_STA_PS &&
  1183. skb_queue_len(&sta->tx_filtered) <
  1184. STA_MAX_TX_BUFFER) {
  1185. ieee80211_remove_tx_extra(local, sta->key,
  1186. skb,
  1187. &status->control);
  1188. skb_queue_tail(&sta->tx_filtered, skb);
  1189. } else if (!(sta->flags & WLAN_STA_PS) &&
  1190. !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
  1191. /* Software retry the packet once */
  1192. status->control.flags |= IEEE80211_TXCTL_REQUEUE;
  1193. ieee80211_remove_tx_extra(local, sta->key,
  1194. skb,
  1195. &status->control);
  1196. dev_queue_xmit(skb);
  1197. } else {
  1198. if (net_ratelimit()) {
  1199. printk(KERN_DEBUG "%s: dropped TX "
  1200. "filtered frame queue_len=%d "
  1201. "PS=%d @%lu\n",
  1202. local->mdev->name,
  1203. skb_queue_len(
  1204. &sta->tx_filtered),
  1205. !!(sta->flags & WLAN_STA_PS),
  1206. jiffies);
  1207. }
  1208. dev_kfree_skb(skb);
  1209. }
  1210. sta_info_put(sta);
  1211. return;
  1212. }
  1213. } else {
  1214. /* FIXME: STUPID to call this with both local and local->mdev */
  1215. rate_control_tx_status(local, local->mdev, skb, status);
  1216. }
  1217. ieee80211_led_tx(local, 0);
  1218. /* SNMP counters
  1219. * Fragments are passed to low-level drivers as separate skbs, so these
  1220. * are actually fragments, not frames. Update frame counters only for
  1221. * the first fragment of the frame. */
  1222. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  1223. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  1224. if (status->flags & IEEE80211_TX_STATUS_ACK) {
  1225. if (frag == 0) {
  1226. local->dot11TransmittedFrameCount++;
  1227. if (is_multicast_ether_addr(hdr->addr1))
  1228. local->dot11MulticastTransmittedFrameCount++;
  1229. if (status->retry_count > 0)
  1230. local->dot11RetryCount++;
  1231. if (status->retry_count > 1)
  1232. local->dot11MultipleRetryCount++;
  1233. }
  1234. /* This counter shall be incremented for an acknowledged MPDU
  1235. * with an individual address in the address 1 field or an MPDU
  1236. * with a multicast address in the address 1 field of type Data
  1237. * or Management. */
  1238. if (!is_multicast_ether_addr(hdr->addr1) ||
  1239. type == IEEE80211_FTYPE_DATA ||
  1240. type == IEEE80211_FTYPE_MGMT)
  1241. local->dot11TransmittedFragmentCount++;
  1242. } else {
  1243. if (frag == 0)
  1244. local->dot11FailedCount++;
  1245. }
  1246. msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
  1247. ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
  1248. /* this was a transmitted frame, but now we want to reuse it */
  1249. skb_orphan(skb);
  1250. if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
  1251. local->apdev) {
  1252. if (local->monitors) {
  1253. skb2 = skb_clone(skb, GFP_ATOMIC);
  1254. } else {
  1255. skb2 = skb;
  1256. skb = NULL;
  1257. }
  1258. if (skb2)
  1259. /* Send frame to hostapd */
  1260. ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
  1261. if (!skb)
  1262. return;
  1263. }
  1264. if (!local->monitors) {
  1265. dev_kfree_skb(skb);
  1266. return;
  1267. }
  1268. /* send frame to monitor interfaces now */
  1269. if (skb_headroom(skb) < sizeof(*rthdr)) {
  1270. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  1271. dev_kfree_skb(skb);
  1272. return;
  1273. }
  1274. rthdr = (struct ieee80211_tx_status_rtap_hdr*)
  1275. skb_push(skb, sizeof(*rthdr));
  1276. memset(rthdr, 0, sizeof(*rthdr));
  1277. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  1278. rthdr->hdr.it_present =
  1279. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  1280. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  1281. if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
  1282. !is_multicast_ether_addr(hdr->addr1))
  1283. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  1284. if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
  1285. (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
  1286. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  1287. else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
  1288. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  1289. rthdr->data_retries = status->retry_count;
  1290. read_lock(&local->sub_if_lock);
  1291. monitors = local->monitors;
  1292. list_for_each_entry(sdata, &local->sub_if_list, list) {
  1293. /*
  1294. * Using the monitors counter is possibly racy, but
  1295. * if the value is wrong we simply either clone the skb
  1296. * once too much or forget sending it to one monitor iface
  1297. * The latter case isn't nice but fixing the race is much
  1298. * more complicated.
  1299. */
  1300. if (!monitors || !skb)
  1301. goto out;
  1302. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  1303. if (!netif_running(sdata->dev))
  1304. continue;
  1305. monitors--;
  1306. if (monitors)
  1307. skb2 = skb_clone(skb, GFP_KERNEL);
  1308. else
  1309. skb2 = NULL;
  1310. skb->dev = sdata->dev;
  1311. /* XXX: is this sufficient for BPF? */
  1312. skb_set_mac_header(skb, 0);
  1313. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1314. skb->pkt_type = PACKET_OTHERHOST;
  1315. skb->protocol = htons(ETH_P_802_2);
  1316. memset(skb->cb, 0, sizeof(skb->cb));
  1317. netif_rx(skb);
  1318. skb = skb2;
  1319. }
  1320. }
  1321. out:
  1322. read_unlock(&local->sub_if_lock);
  1323. if (skb)
  1324. dev_kfree_skb(skb);
  1325. }
  1326. EXPORT_SYMBOL(ieee80211_tx_status);
  1327. int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
  1328. {
  1329. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1330. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1331. struct sta_info *sta;
  1332. if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
  1333. return 0;
  1334. /* Create STA entry for the new peer */
  1335. sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
  1336. if (!sta)
  1337. return -ENOMEM;
  1338. sta_info_put(sta);
  1339. /* Remove STA entry for the old peer */
  1340. sta = sta_info_get(local, sdata->u.wds.remote_addr);
  1341. if (sta) {
  1342. sta_info_put(sta);
  1343. sta_info_free(sta, 0);
  1344. } else {
  1345. printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
  1346. "peer " MAC_FMT "\n",
  1347. dev->name, MAC_ARG(sdata->u.wds.remote_addr));
  1348. }
  1349. /* Update WDS link data */
  1350. memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
  1351. return 0;
  1352. }
  1353. /* Must not be called for mdev and apdev */
  1354. void ieee80211_if_setup(struct net_device *dev)
  1355. {
  1356. ether_setup(dev);
  1357. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  1358. dev->wireless_handlers = &ieee80211_iw_handler_def;
  1359. dev->set_multicast_list = ieee80211_set_multicast_list;
  1360. dev->change_mtu = ieee80211_change_mtu;
  1361. dev->get_stats = ieee80211_get_stats;
  1362. dev->open = ieee80211_open;
  1363. dev->stop = ieee80211_stop;
  1364. dev->uninit = ieee80211_if_reinit;
  1365. dev->destructor = ieee80211_if_free;
  1366. }
  1367. void ieee80211_if_mgmt_setup(struct net_device *dev)
  1368. {
  1369. ether_setup(dev);
  1370. dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
  1371. dev->change_mtu = ieee80211_change_mtu_apdev;
  1372. dev->get_stats = ieee80211_get_stats;
  1373. dev->open = ieee80211_mgmt_open;
  1374. dev->stop = ieee80211_mgmt_stop;
  1375. dev->type = ARPHRD_IEEE80211_PRISM;
  1376. dev->hard_header_parse = header_parse_80211;
  1377. dev->uninit = ieee80211_if_reinit;
  1378. dev->destructor = ieee80211_if_free;
  1379. }
  1380. int ieee80211_init_rate_ctrl_alg(struct ieee80211_local *local,
  1381. const char *name)
  1382. {
  1383. struct rate_control_ref *ref, *old;
  1384. ASSERT_RTNL();
  1385. if (local->open_count || netif_running(local->mdev) ||
  1386. (local->apdev && netif_running(local->apdev)))
  1387. return -EBUSY;
  1388. ref = rate_control_alloc(name, local);
  1389. if (!ref) {
  1390. printk(KERN_WARNING "%s: Failed to select rate control "
  1391. "algorithm\n", local->mdev->name);
  1392. return -ENOENT;
  1393. }
  1394. old = local->rate_ctrl;
  1395. local->rate_ctrl = ref;
  1396. if (old) {
  1397. rate_control_put(old);
  1398. sta_info_flush(local, NULL);
  1399. }
  1400. printk(KERN_DEBUG "%s: Selected rate control "
  1401. "algorithm '%s'\n", local->mdev->name,
  1402. ref->ops->name);
  1403. return 0;
  1404. }
  1405. static void rate_control_deinitialize(struct ieee80211_local *local)
  1406. {
  1407. struct rate_control_ref *ref;
  1408. ref = local->rate_ctrl;
  1409. local->rate_ctrl = NULL;
  1410. rate_control_put(ref);
  1411. }
  1412. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  1413. const struct ieee80211_ops *ops)
  1414. {
  1415. struct net_device *mdev;
  1416. struct ieee80211_local *local;
  1417. struct ieee80211_sub_if_data *sdata;
  1418. int priv_size;
  1419. struct wiphy *wiphy;
  1420. /* Ensure 32-byte alignment of our private data and hw private data.
  1421. * We use the wiphy priv data for both our ieee80211_local and for
  1422. * the driver's private data
  1423. *
  1424. * In memory it'll be like this:
  1425. *
  1426. * +-------------------------+
  1427. * | struct wiphy |
  1428. * +-------------------------+
  1429. * | struct ieee80211_local |
  1430. * +-------------------------+
  1431. * | driver's private data |
  1432. * +-------------------------+
  1433. *
  1434. */
  1435. priv_size = ((sizeof(struct ieee80211_local) +
  1436. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  1437. priv_data_len;
  1438. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  1439. if (!wiphy)
  1440. return NULL;
  1441. wiphy->privid = mac80211_wiphy_privid;
  1442. local = wiphy_priv(wiphy);
  1443. local->hw.wiphy = wiphy;
  1444. local->hw.priv = (char *)local +
  1445. ((sizeof(struct ieee80211_local) +
  1446. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  1447. BUG_ON(!ops->tx);
  1448. BUG_ON(!ops->config);
  1449. BUG_ON(!ops->add_interface);
  1450. local->ops = ops;
  1451. /* for now, mdev needs sub_if_data :/ */
  1452. mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
  1453. "wmaster%d", ether_setup);
  1454. if (!mdev) {
  1455. wiphy_free(wiphy);
  1456. return NULL;
  1457. }
  1458. sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
  1459. mdev->ieee80211_ptr = &sdata->wdev;
  1460. sdata->wdev.wiphy = wiphy;
  1461. local->hw.queues = 1; /* default */
  1462. local->mdev = mdev;
  1463. local->rx_pre_handlers = ieee80211_rx_pre_handlers;
  1464. local->rx_handlers = ieee80211_rx_handlers;
  1465. local->tx_handlers = ieee80211_tx_handlers;
  1466. local->bridge_packets = 1;
  1467. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  1468. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  1469. local->short_retry_limit = 7;
  1470. local->long_retry_limit = 4;
  1471. local->hw.conf.radio_enabled = 1;
  1472. local->enabled_modes = (unsigned int) -1;
  1473. INIT_LIST_HEAD(&local->modes_list);
  1474. rwlock_init(&local->sub_if_lock);
  1475. INIT_LIST_HEAD(&local->sub_if_list);
  1476. INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
  1477. init_timer(&local->stat_timer);
  1478. local->stat_timer.function = ieee80211_stat_refresh;
  1479. local->stat_timer.data = (unsigned long) local;
  1480. ieee80211_rx_bss_list_init(mdev);
  1481. sta_info_init(local);
  1482. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  1483. mdev->open = ieee80211_master_open;
  1484. mdev->stop = ieee80211_master_stop;
  1485. mdev->type = ARPHRD_IEEE80211;
  1486. mdev->hard_header_parse = header_parse_80211;
  1487. sdata->type = IEEE80211_IF_TYPE_AP;
  1488. sdata->dev = mdev;
  1489. sdata->local = local;
  1490. sdata->u.ap.force_unicast_rateidx = -1;
  1491. sdata->u.ap.max_ratectrl_rateidx = -1;
  1492. ieee80211_if_sdata_init(sdata);
  1493. list_add_tail(&sdata->list, &local->sub_if_list);
  1494. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  1495. (unsigned long)local);
  1496. tasklet_disable(&local->tx_pending_tasklet);
  1497. tasklet_init(&local->tasklet,
  1498. ieee80211_tasklet_handler,
  1499. (unsigned long) local);
  1500. tasklet_disable(&local->tasklet);
  1501. skb_queue_head_init(&local->skb_queue);
  1502. skb_queue_head_init(&local->skb_queue_unreliable);
  1503. return local_to_hw(local);
  1504. }
  1505. EXPORT_SYMBOL(ieee80211_alloc_hw);
  1506. int ieee80211_register_hw(struct ieee80211_hw *hw)
  1507. {
  1508. struct ieee80211_local *local = hw_to_local(hw);
  1509. const char *name;
  1510. int result;
  1511. result = wiphy_register(local->hw.wiphy);
  1512. if (result < 0)
  1513. return result;
  1514. name = wiphy_dev(local->hw.wiphy)->driver->name;
  1515. local->hw.workqueue = create_singlethread_workqueue(name);
  1516. if (!local->hw.workqueue) {
  1517. result = -ENOMEM;
  1518. goto fail_workqueue;
  1519. }
  1520. /*
  1521. * The hardware needs headroom for sending the frame,
  1522. * and we need some headroom for passing the frame to monitor
  1523. * interfaces, but never both at the same time.
  1524. */
  1525. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  1526. sizeof(struct ieee80211_tx_status_rtap_hdr));
  1527. debugfs_hw_add(local);
  1528. local->hw.conf.beacon_int = 1000;
  1529. local->wstats_flags |= local->hw.max_rssi ?
  1530. IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
  1531. local->wstats_flags |= local->hw.max_signal ?
  1532. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  1533. local->wstats_flags |= local->hw.max_noise ?
  1534. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  1535. if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
  1536. local->wstats_flags |= IW_QUAL_DBM;
  1537. result = sta_info_start(local);
  1538. if (result < 0)
  1539. goto fail_sta_info;
  1540. rtnl_lock();
  1541. result = dev_alloc_name(local->mdev, local->mdev->name);
  1542. if (result < 0)
  1543. goto fail_dev;
  1544. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1545. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  1546. result = register_netdevice(local->mdev);
  1547. if (result < 0)
  1548. goto fail_dev;
  1549. ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1550. result = ieee80211_init_rate_ctrl_alg(local, NULL);
  1551. if (result < 0) {
  1552. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  1553. "algorithm\n", local->mdev->name);
  1554. goto fail_rate;
  1555. }
  1556. result = ieee80211_wep_init(local);
  1557. if (result < 0) {
  1558. printk(KERN_DEBUG "%s: Failed to initialize wep\n",
  1559. local->mdev->name);
  1560. goto fail_wep;
  1561. }
  1562. ieee80211_install_qdisc(local->mdev);
  1563. /* add one default STA interface */
  1564. result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
  1565. IEEE80211_IF_TYPE_STA);
  1566. if (result)
  1567. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  1568. local->mdev->name);
  1569. local->reg_state = IEEE80211_DEV_REGISTERED;
  1570. rtnl_unlock();
  1571. ieee80211_led_init(local);
  1572. return 0;
  1573. fail_wep:
  1574. rate_control_deinitialize(local);
  1575. fail_rate:
  1576. ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1577. unregister_netdevice(local->mdev);
  1578. fail_dev:
  1579. rtnl_unlock();
  1580. sta_info_stop(local);
  1581. fail_sta_info:
  1582. debugfs_hw_del(local);
  1583. destroy_workqueue(local->hw.workqueue);
  1584. fail_workqueue:
  1585. wiphy_unregister(local->hw.wiphy);
  1586. return result;
  1587. }
  1588. EXPORT_SYMBOL(ieee80211_register_hw);
  1589. int ieee80211_register_hwmode(struct ieee80211_hw *hw,
  1590. struct ieee80211_hw_mode *mode)
  1591. {
  1592. struct ieee80211_local *local = hw_to_local(hw);
  1593. struct ieee80211_rate *rate;
  1594. int i;
  1595. INIT_LIST_HEAD(&mode->list);
  1596. list_add_tail(&mode->list, &local->modes_list);
  1597. local->hw_modes |= (1 << mode->mode);
  1598. for (i = 0; i < mode->num_rates; i++) {
  1599. rate = &(mode->rates[i]);
  1600. rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
  1601. }
  1602. ieee80211_prepare_rates(local, mode);
  1603. if (!local->oper_hw_mode) {
  1604. /* Default to this mode */
  1605. local->hw.conf.phymode = mode->mode;
  1606. local->oper_hw_mode = local->scan_hw_mode = mode;
  1607. local->oper_channel = local->scan_channel = &mode->channels[0];
  1608. local->hw.conf.mode = local->oper_hw_mode;
  1609. local->hw.conf.chan = local->oper_channel;
  1610. }
  1611. if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
  1612. ieee80211_set_default_regdomain(mode);
  1613. return 0;
  1614. }
  1615. EXPORT_SYMBOL(ieee80211_register_hwmode);
  1616. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  1617. {
  1618. struct ieee80211_local *local = hw_to_local(hw);
  1619. struct ieee80211_sub_if_data *sdata, *tmp;
  1620. struct list_head tmp_list;
  1621. int i;
  1622. tasklet_kill(&local->tx_pending_tasklet);
  1623. tasklet_kill(&local->tasklet);
  1624. rtnl_lock();
  1625. BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
  1626. local->reg_state = IEEE80211_DEV_UNREGISTERED;
  1627. if (local->apdev)
  1628. ieee80211_if_del_mgmt(local);
  1629. write_lock_bh(&local->sub_if_lock);
  1630. list_replace_init(&local->sub_if_list, &tmp_list);
  1631. write_unlock_bh(&local->sub_if_lock);
  1632. list_for_each_entry_safe(sdata, tmp, &tmp_list, list)
  1633. __ieee80211_if_del(local, sdata);
  1634. rtnl_unlock();
  1635. if (local->stat_time)
  1636. del_timer_sync(&local->stat_timer);
  1637. ieee80211_rx_bss_list_deinit(local->mdev);
  1638. ieee80211_clear_tx_pending(local);
  1639. sta_info_stop(local);
  1640. rate_control_deinitialize(local);
  1641. debugfs_hw_del(local);
  1642. for (i = 0; i < NUM_IEEE80211_MODES; i++) {
  1643. kfree(local->supp_rates[i]);
  1644. kfree(local->basic_rates[i]);
  1645. }
  1646. if (skb_queue_len(&local->skb_queue)
  1647. || skb_queue_len(&local->skb_queue_unreliable))
  1648. printk(KERN_WARNING "%s: skb_queue not empty\n",
  1649. local->mdev->name);
  1650. skb_queue_purge(&local->skb_queue);
  1651. skb_queue_purge(&local->skb_queue_unreliable);
  1652. destroy_workqueue(local->hw.workqueue);
  1653. wiphy_unregister(local->hw.wiphy);
  1654. ieee80211_wep_free(local);
  1655. ieee80211_led_exit(local);
  1656. }
  1657. EXPORT_SYMBOL(ieee80211_unregister_hw);
  1658. void ieee80211_free_hw(struct ieee80211_hw *hw)
  1659. {
  1660. struct ieee80211_local *local = hw_to_local(hw);
  1661. ieee80211_if_free(local->mdev);
  1662. wiphy_free(local->hw.wiphy);
  1663. }
  1664. EXPORT_SYMBOL(ieee80211_free_hw);
  1665. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  1666. {
  1667. struct ieee80211_local *local = hw_to_local(hw);
  1668. if (test_and_clear_bit(IEEE80211_LINK_STATE_XOFF,
  1669. &local->state[queue])) {
  1670. if (test_bit(IEEE80211_LINK_STATE_PENDING,
  1671. &local->state[queue]))
  1672. tasklet_schedule(&local->tx_pending_tasklet);
  1673. else
  1674. if (!ieee80211_qdisc_installed(local->mdev)) {
  1675. if (queue == 0)
  1676. netif_wake_queue(local->mdev);
  1677. } else
  1678. __netif_schedule(local->mdev);
  1679. }
  1680. }
  1681. EXPORT_SYMBOL(ieee80211_wake_queue);
  1682. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  1683. {
  1684. struct ieee80211_local *local = hw_to_local(hw);
  1685. if (!ieee80211_qdisc_installed(local->mdev) && queue == 0)
  1686. netif_stop_queue(local->mdev);
  1687. set_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
  1688. }
  1689. EXPORT_SYMBOL(ieee80211_stop_queue);
  1690. void ieee80211_start_queues(struct ieee80211_hw *hw)
  1691. {
  1692. struct ieee80211_local *local = hw_to_local(hw);
  1693. int i;
  1694. for (i = 0; i < local->hw.queues; i++)
  1695. clear_bit(IEEE80211_LINK_STATE_XOFF, &local->state[i]);
  1696. if (!ieee80211_qdisc_installed(local->mdev))
  1697. netif_start_queue(local->mdev);
  1698. }
  1699. EXPORT_SYMBOL(ieee80211_start_queues);
  1700. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  1701. {
  1702. int i;
  1703. for (i = 0; i < hw->queues; i++)
  1704. ieee80211_stop_queue(hw, i);
  1705. }
  1706. EXPORT_SYMBOL(ieee80211_stop_queues);
  1707. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  1708. {
  1709. int i;
  1710. for (i = 0; i < hw->queues; i++)
  1711. ieee80211_wake_queue(hw, i);
  1712. }
  1713. EXPORT_SYMBOL(ieee80211_wake_queues);
  1714. struct net_device_stats *ieee80211_dev_stats(struct net_device *dev)
  1715. {
  1716. struct ieee80211_sub_if_data *sdata;
  1717. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1718. return &sdata->stats;
  1719. }
  1720. static int __init ieee80211_init(void)
  1721. {
  1722. struct sk_buff *skb;
  1723. int ret;
  1724. BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
  1725. ret = ieee80211_wme_register();
  1726. if (ret) {
  1727. printk(KERN_DEBUG "ieee80211_init: failed to "
  1728. "initialize WME (err=%d)\n", ret);
  1729. return ret;
  1730. }
  1731. ieee80211_debugfs_netdev_init();
  1732. ieee80211_regdomain_init();
  1733. return 0;
  1734. }
  1735. static void __exit ieee80211_exit(void)
  1736. {
  1737. ieee80211_wme_unregister();
  1738. ieee80211_debugfs_netdev_exit();
  1739. }
  1740. subsys_initcall(ieee80211_init);
  1741. module_exit(ieee80211_exit);
  1742. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  1743. MODULE_LICENSE("GPL");