ieee80211.c 53 KB

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