main.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include "ieee80211_i.h"
  26. #include "rate.h"
  27. #include "mesh.h"
  28. #include "wep.h"
  29. #include "wme.h"
  30. #include "aes_ccm.h"
  31. #include "led.h"
  32. #include "cfg.h"
  33. #include "debugfs.h"
  34. #include "debugfs_netdev.h"
  35. /*
  36. * For seeing transmitted packets on monitor interfaces
  37. * we have a radiotap header too.
  38. */
  39. struct ieee80211_tx_status_rtap_hdr {
  40. struct ieee80211_radiotap_header hdr;
  41. __le16 tx_flags;
  42. u8 data_retries;
  43. } __attribute__ ((packed));
  44. /* common interface routines */
  45. static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
  46. {
  47. memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
  48. return ETH_ALEN;
  49. }
  50. /* must be called under mdev tx lock */
  51. static void ieee80211_configure_filter(struct ieee80211_local *local)
  52. {
  53. unsigned int changed_flags;
  54. unsigned int new_flags = 0;
  55. if (atomic_read(&local->iff_promiscs))
  56. new_flags |= FIF_PROMISC_IN_BSS;
  57. if (atomic_read(&local->iff_allmultis))
  58. new_flags |= FIF_ALLMULTI;
  59. if (local->monitors)
  60. new_flags |= FIF_BCN_PRBRESP_PROMISC;
  61. if (local->fif_fcsfail)
  62. new_flags |= FIF_FCSFAIL;
  63. if (local->fif_plcpfail)
  64. new_flags |= FIF_PLCPFAIL;
  65. if (local->fif_control)
  66. new_flags |= FIF_CONTROL;
  67. if (local->fif_other_bss)
  68. new_flags |= FIF_OTHER_BSS;
  69. changed_flags = local->filter_flags ^ new_flags;
  70. /* be a bit nasty */
  71. new_flags |= (1<<31);
  72. local->ops->configure_filter(local_to_hw(local),
  73. changed_flags, &new_flags,
  74. local->mdev->mc_count,
  75. local->mdev->mc_list);
  76. WARN_ON(new_flags & (1<<31));
  77. local->filter_flags = new_flags & ~(1<<31);
  78. }
  79. /* master interface */
  80. static int ieee80211_master_open(struct net_device *dev)
  81. {
  82. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  83. struct ieee80211_sub_if_data *sdata;
  84. int res = -EOPNOTSUPP;
  85. /* we hold the RTNL here so can safely walk the list */
  86. list_for_each_entry(sdata, &local->interfaces, list) {
  87. if (netif_running(sdata->dev)) {
  88. res = 0;
  89. break;
  90. }
  91. }
  92. if (res)
  93. return res;
  94. netif_tx_start_all_queues(local->mdev);
  95. return 0;
  96. }
  97. static int ieee80211_master_stop(struct net_device *dev)
  98. {
  99. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  100. struct ieee80211_sub_if_data *sdata;
  101. /* we hold the RTNL here so can safely walk the list */
  102. list_for_each_entry(sdata, &local->interfaces, list)
  103. if (netif_running(sdata->dev))
  104. dev_close(sdata->dev);
  105. return 0;
  106. }
  107. static void ieee80211_master_set_multicast_list(struct net_device *dev)
  108. {
  109. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  110. ieee80211_configure_filter(local);
  111. }
  112. /* regular interfaces */
  113. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  114. {
  115. int meshhdrlen;
  116. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  117. meshhdrlen = (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) ? 5 : 0;
  118. /* FIX: what would be proper limits for MTU?
  119. * This interface uses 802.3 frames. */
  120. if (new_mtu < 256 ||
  121. new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
  122. return -EINVAL;
  123. }
  124. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  125. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  126. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  127. dev->mtu = new_mtu;
  128. return 0;
  129. }
  130. static inline int identical_mac_addr_allowed(int type1, int type2)
  131. {
  132. return type1 == NL80211_IFTYPE_MONITOR ||
  133. type2 == NL80211_IFTYPE_MONITOR ||
  134. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
  135. (type1 == NL80211_IFTYPE_WDS &&
  136. (type2 == NL80211_IFTYPE_WDS ||
  137. type2 == NL80211_IFTYPE_AP)) ||
  138. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
  139. (type1 == NL80211_IFTYPE_AP_VLAN &&
  140. (type2 == NL80211_IFTYPE_AP ||
  141. type2 == NL80211_IFTYPE_AP_VLAN));
  142. }
  143. static int ieee80211_open(struct net_device *dev)
  144. {
  145. struct ieee80211_sub_if_data *sdata, *nsdata;
  146. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  147. struct sta_info *sta;
  148. struct ieee80211_if_init_conf conf;
  149. u32 changed = 0;
  150. int res;
  151. bool need_hw_reconfig = 0;
  152. u8 null_addr[ETH_ALEN] = {0};
  153. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  154. /* fail early if user set an invalid address */
  155. if (compare_ether_addr(dev->dev_addr, null_addr) &&
  156. !is_valid_ether_addr(dev->dev_addr))
  157. return -EADDRNOTAVAIL;
  158. /* we hold the RTNL here so can safely walk the list */
  159. list_for_each_entry(nsdata, &local->interfaces, list) {
  160. struct net_device *ndev = nsdata->dev;
  161. if (ndev != dev && netif_running(ndev)) {
  162. /*
  163. * Allow only a single IBSS interface to be up at any
  164. * time. This is restricted because beacon distribution
  165. * cannot work properly if both are in the same IBSS.
  166. *
  167. * To remove this restriction we'd have to disallow them
  168. * from setting the same SSID on different IBSS interfaces
  169. * belonging to the same hardware. Then, however, we're
  170. * faced with having to adopt two different TSF timers...
  171. */
  172. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  173. nsdata->vif.type == NL80211_IFTYPE_ADHOC)
  174. return -EBUSY;
  175. /*
  176. * The remaining checks are only performed for interfaces
  177. * with the same MAC address.
  178. */
  179. if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
  180. continue;
  181. /*
  182. * check whether it may have the same address
  183. */
  184. if (!identical_mac_addr_allowed(sdata->vif.type,
  185. nsdata->vif.type))
  186. return -ENOTUNIQ;
  187. /*
  188. * can only add VLANs to enabled APs
  189. */
  190. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  191. nsdata->vif.type == NL80211_IFTYPE_AP)
  192. sdata->bss = &nsdata->u.ap;
  193. }
  194. }
  195. switch (sdata->vif.type) {
  196. case NL80211_IFTYPE_WDS:
  197. if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
  198. return -ENOLINK;
  199. break;
  200. case NL80211_IFTYPE_AP_VLAN:
  201. if (!sdata->bss)
  202. return -ENOLINK;
  203. list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
  204. break;
  205. case NL80211_IFTYPE_AP:
  206. sdata->bss = &sdata->u.ap;
  207. break;
  208. case NL80211_IFTYPE_MESH_POINT:
  209. if (!ieee80211_vif_is_mesh(&sdata->vif))
  210. break;
  211. /* mesh ifaces must set allmulti to forward mcast traffic */
  212. atomic_inc(&local->iff_allmultis);
  213. break;
  214. case NL80211_IFTYPE_STATION:
  215. case NL80211_IFTYPE_MONITOR:
  216. case NL80211_IFTYPE_ADHOC:
  217. /* no special treatment */
  218. break;
  219. case NL80211_IFTYPE_UNSPECIFIED:
  220. case __NL80211_IFTYPE_AFTER_LAST:
  221. /* cannot happen */
  222. WARN_ON(1);
  223. break;
  224. }
  225. if (local->open_count == 0) {
  226. res = 0;
  227. if (local->ops->start)
  228. res = local->ops->start(local_to_hw(local));
  229. if (res)
  230. goto err_del_bss;
  231. need_hw_reconfig = 1;
  232. ieee80211_led_radio(local, local->hw.conf.radio_enabled);
  233. }
  234. /*
  235. * Check all interfaces and copy the hopefully now-present
  236. * MAC address to those that have the special null one.
  237. */
  238. list_for_each_entry(nsdata, &local->interfaces, list) {
  239. struct net_device *ndev = nsdata->dev;
  240. /*
  241. * No need to check netif_running since we do not allow
  242. * it to start up with this invalid address.
  243. */
  244. if (compare_ether_addr(null_addr, ndev->dev_addr) == 0)
  245. memcpy(ndev->dev_addr,
  246. local->hw.wiphy->perm_addr,
  247. ETH_ALEN);
  248. }
  249. if (compare_ether_addr(null_addr, local->mdev->dev_addr) == 0)
  250. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr,
  251. ETH_ALEN);
  252. /*
  253. * Validate the MAC address for this device.
  254. */
  255. if (!is_valid_ether_addr(dev->dev_addr)) {
  256. if (!local->open_count && local->ops->stop)
  257. local->ops->stop(local_to_hw(local));
  258. return -EADDRNOTAVAIL;
  259. }
  260. switch (sdata->vif.type) {
  261. case NL80211_IFTYPE_AP_VLAN:
  262. /* no need to tell driver */
  263. break;
  264. case NL80211_IFTYPE_MONITOR:
  265. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  266. local->cooked_mntrs++;
  267. break;
  268. }
  269. /* must be before the call to ieee80211_configure_filter */
  270. local->monitors++;
  271. if (local->monitors == 1)
  272. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  273. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  274. local->fif_fcsfail++;
  275. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  276. local->fif_plcpfail++;
  277. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
  278. local->fif_control++;
  279. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  280. local->fif_other_bss++;
  281. netif_addr_lock_bh(local->mdev);
  282. ieee80211_configure_filter(local);
  283. netif_addr_unlock_bh(local->mdev);
  284. break;
  285. case NL80211_IFTYPE_STATION:
  286. case NL80211_IFTYPE_ADHOC:
  287. sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  288. /* fall through */
  289. default:
  290. conf.vif = &sdata->vif;
  291. conf.type = sdata->vif.type;
  292. conf.mac_addr = dev->dev_addr;
  293. res = local->ops->add_interface(local_to_hw(local), &conf);
  294. if (res)
  295. goto err_stop;
  296. if (ieee80211_vif_is_mesh(&sdata->vif))
  297. ieee80211_start_mesh(sdata);
  298. changed |= ieee80211_reset_erp_info(sdata);
  299. ieee80211_bss_info_change_notify(sdata, changed);
  300. ieee80211_enable_keys(sdata);
  301. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  302. !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
  303. netif_carrier_off(dev);
  304. else
  305. netif_carrier_on(dev);
  306. }
  307. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  308. /* Create STA entry for the WDS peer */
  309. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  310. GFP_KERNEL);
  311. if (!sta) {
  312. res = -ENOMEM;
  313. goto err_del_interface;
  314. }
  315. /* no locking required since STA is not live yet */
  316. sta->flags |= WLAN_STA_AUTHORIZED;
  317. res = sta_info_insert(sta);
  318. if (res) {
  319. /* STA has been freed */
  320. goto err_del_interface;
  321. }
  322. }
  323. if (local->open_count == 0) {
  324. res = dev_open(local->mdev);
  325. WARN_ON(res);
  326. if (res)
  327. goto err_del_interface;
  328. tasklet_enable(&local->tx_pending_tasklet);
  329. tasklet_enable(&local->tasklet);
  330. }
  331. /*
  332. * set_multicast_list will be invoked by the networking core
  333. * which will check whether any increments here were done in
  334. * error and sync them down to the hardware as filter flags.
  335. */
  336. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  337. atomic_inc(&local->iff_allmultis);
  338. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  339. atomic_inc(&local->iff_promiscs);
  340. local->open_count++;
  341. if (need_hw_reconfig) {
  342. ieee80211_hw_config(local);
  343. /*
  344. * set default queue parameters so drivers don't
  345. * need to initialise the hardware if the hardware
  346. * doesn't start up with sane defaults
  347. */
  348. ieee80211_set_wmm_default(sdata);
  349. }
  350. /*
  351. * ieee80211_sta_work is disabled while network interface
  352. * is down. Therefore, some configuration changes may not
  353. * yet be effective. Trigger execution of ieee80211_sta_work
  354. * to fix this.
  355. */
  356. if (sdata->vif.type == NL80211_IFTYPE_STATION ||
  357. sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  358. struct ieee80211_if_sta *ifsta = &sdata->u.sta;
  359. queue_work(local->hw.workqueue, &ifsta->work);
  360. }
  361. netif_tx_start_all_queues(dev);
  362. return 0;
  363. err_del_interface:
  364. local->ops->remove_interface(local_to_hw(local), &conf);
  365. err_stop:
  366. if (!local->open_count && local->ops->stop)
  367. local->ops->stop(local_to_hw(local));
  368. err_del_bss:
  369. sdata->bss = NULL;
  370. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  371. list_del(&sdata->u.vlan.list);
  372. return res;
  373. }
  374. static int ieee80211_stop(struct net_device *dev)
  375. {
  376. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  377. struct ieee80211_local *local = sdata->local;
  378. struct ieee80211_if_init_conf conf;
  379. struct sta_info *sta;
  380. /*
  381. * Stop TX on this interface first.
  382. */
  383. netif_tx_stop_all_queues(dev);
  384. /*
  385. * Now delete all active aggregation sessions.
  386. */
  387. rcu_read_lock();
  388. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  389. if (sta->sdata == sdata)
  390. ieee80211_sta_tear_down_BA_sessions(sdata, sta->addr);
  391. }
  392. rcu_read_unlock();
  393. /*
  394. * Remove all stations associated with this interface.
  395. *
  396. * This must be done before calling ops->remove_interface()
  397. * because otherwise we can later invoke ops->sta_notify()
  398. * whenever the STAs are removed, and that invalidates driver
  399. * assumptions about always getting a vif pointer that is valid
  400. * (because if we remove a STA after ops->remove_interface()
  401. * the driver will have removed the vif info already!)
  402. *
  403. * We could relax this and only unlink the stations from the
  404. * hash table and list but keep them on a per-sdata list that
  405. * will be inserted back again when the interface is brought
  406. * up again, but I don't currently see a use case for that,
  407. * except with WDS which gets a STA entry created when it is
  408. * brought up.
  409. */
  410. sta_info_flush(local, sdata);
  411. /*
  412. * Don't count this interface for promisc/allmulti while it
  413. * is down. dev_mc_unsync() will invoke set_multicast_list
  414. * on the master interface which will sync these down to the
  415. * hardware as filter flags.
  416. */
  417. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  418. atomic_dec(&local->iff_allmultis);
  419. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  420. atomic_dec(&local->iff_promiscs);
  421. dev_mc_unsync(local->mdev, dev);
  422. /* APs need special treatment */
  423. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  424. struct ieee80211_sub_if_data *vlan, *tmp;
  425. struct beacon_data *old_beacon = sdata->u.ap.beacon;
  426. /* remove beacon */
  427. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  428. synchronize_rcu();
  429. kfree(old_beacon);
  430. /* down all dependent devices, that is VLANs */
  431. list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
  432. u.vlan.list)
  433. dev_close(vlan->dev);
  434. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  435. }
  436. local->open_count--;
  437. switch (sdata->vif.type) {
  438. case NL80211_IFTYPE_AP_VLAN:
  439. list_del(&sdata->u.vlan.list);
  440. /* no need to tell driver */
  441. break;
  442. case NL80211_IFTYPE_MONITOR:
  443. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  444. local->cooked_mntrs--;
  445. break;
  446. }
  447. local->monitors--;
  448. if (local->monitors == 0)
  449. local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
  450. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  451. local->fif_fcsfail--;
  452. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  453. local->fif_plcpfail--;
  454. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
  455. local->fif_control--;
  456. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  457. local->fif_other_bss--;
  458. netif_addr_lock_bh(local->mdev);
  459. ieee80211_configure_filter(local);
  460. netif_addr_unlock_bh(local->mdev);
  461. break;
  462. case NL80211_IFTYPE_STATION:
  463. case NL80211_IFTYPE_ADHOC:
  464. sdata->u.sta.state = IEEE80211_STA_MLME_DISABLED;
  465. memset(sdata->u.sta.bssid, 0, ETH_ALEN);
  466. del_timer_sync(&sdata->u.sta.timer);
  467. /*
  468. * If the timer fired while we waited for it, it will have
  469. * requeued the work. Now the work will be running again
  470. * but will not rearm the timer again because it checks
  471. * whether the interface is running, which, at this point,
  472. * it no longer is.
  473. */
  474. cancel_work_sync(&sdata->u.sta.work);
  475. /*
  476. * When we get here, the interface is marked down.
  477. * Call synchronize_rcu() to wait for the RX path
  478. * should it be using the interface and enqueuing
  479. * frames at this very time on another CPU.
  480. */
  481. synchronize_rcu();
  482. skb_queue_purge(&sdata->u.sta.skb_queue);
  483. sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
  484. kfree(sdata->u.sta.extra_ie);
  485. sdata->u.sta.extra_ie = NULL;
  486. sdata->u.sta.extra_ie_len = 0;
  487. /* fall through */
  488. case NL80211_IFTYPE_MESH_POINT:
  489. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  490. /* allmulti is always set on mesh ifaces */
  491. atomic_dec(&local->iff_allmultis);
  492. ieee80211_stop_mesh(sdata);
  493. }
  494. /* fall through */
  495. default:
  496. if (local->scan_sdata == sdata) {
  497. if (!local->ops->hw_scan)
  498. cancel_delayed_work_sync(&local->scan_work);
  499. /*
  500. * The software scan can no longer run now, so we can
  501. * clear out the scan_sdata reference. However, the
  502. * hardware scan may still be running. The complete
  503. * function must be prepared to handle a NULL value.
  504. */
  505. local->scan_sdata = NULL;
  506. /*
  507. * The memory barrier guarantees that another CPU
  508. * that is hardware-scanning will now see the fact
  509. * that this interface is gone.
  510. */
  511. smp_mb();
  512. /*
  513. * If software scanning, complete the scan but since
  514. * the scan_sdata is NULL already don't send out a
  515. * scan event to userspace -- the scan is incomplete.
  516. */
  517. if (local->sw_scanning)
  518. ieee80211_scan_completed(&local->hw);
  519. }
  520. conf.vif = &sdata->vif;
  521. conf.type = sdata->vif.type;
  522. conf.mac_addr = dev->dev_addr;
  523. /* disable all keys for as long as this netdev is down */
  524. ieee80211_disable_keys(sdata);
  525. local->ops->remove_interface(local_to_hw(local), &conf);
  526. }
  527. sdata->bss = NULL;
  528. if (local->open_count == 0) {
  529. if (netif_running(local->mdev))
  530. dev_close(local->mdev);
  531. if (local->ops->stop)
  532. local->ops->stop(local_to_hw(local));
  533. ieee80211_led_radio(local, 0);
  534. flush_workqueue(local->hw.workqueue);
  535. tasklet_disable(&local->tx_pending_tasklet);
  536. tasklet_disable(&local->tasklet);
  537. }
  538. return 0;
  539. }
  540. static void ieee80211_set_multicast_list(struct net_device *dev)
  541. {
  542. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  543. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  544. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  545. allmulti = !!(dev->flags & IFF_ALLMULTI);
  546. promisc = !!(dev->flags & IFF_PROMISC);
  547. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  548. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  549. if (allmulti != sdata_allmulti) {
  550. if (dev->flags & IFF_ALLMULTI)
  551. atomic_inc(&local->iff_allmultis);
  552. else
  553. atomic_dec(&local->iff_allmultis);
  554. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  555. }
  556. if (promisc != sdata_promisc) {
  557. if (dev->flags & IFF_PROMISC)
  558. atomic_inc(&local->iff_promiscs);
  559. else
  560. atomic_dec(&local->iff_promiscs);
  561. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  562. }
  563. dev_mc_sync(local->mdev, dev);
  564. }
  565. static const struct header_ops ieee80211_header_ops = {
  566. .create = eth_header,
  567. .parse = header_parse_80211,
  568. .rebuild = eth_rebuild_header,
  569. .cache = eth_header_cache,
  570. .cache_update = eth_header_cache_update,
  571. };
  572. void ieee80211_if_setup(struct net_device *dev)
  573. {
  574. ether_setup(dev);
  575. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  576. dev->wireless_handlers = &ieee80211_iw_handler_def;
  577. dev->set_multicast_list = ieee80211_set_multicast_list;
  578. dev->change_mtu = ieee80211_change_mtu;
  579. dev->open = ieee80211_open;
  580. dev->stop = ieee80211_stop;
  581. dev->destructor = free_netdev;
  582. /* we will validate the address ourselves in ->open */
  583. dev->validate_addr = NULL;
  584. }
  585. /* everything else */
  586. int ieee80211_if_config(struct ieee80211_sub_if_data *sdata, u32 changed)
  587. {
  588. struct ieee80211_local *local = sdata->local;
  589. struct ieee80211_if_conf conf;
  590. if (WARN_ON(!netif_running(sdata->dev)))
  591. return 0;
  592. if (!local->ops->config_interface)
  593. return 0;
  594. memset(&conf, 0, sizeof(conf));
  595. conf.changed = changed;
  596. if (sdata->vif.type == NL80211_IFTYPE_STATION ||
  597. sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  598. conf.bssid = sdata->u.sta.bssid;
  599. conf.ssid = sdata->u.sta.ssid;
  600. conf.ssid_len = sdata->u.sta.ssid_len;
  601. } else if (sdata->vif.type == NL80211_IFTYPE_AP) {
  602. conf.bssid = sdata->dev->dev_addr;
  603. conf.ssid = sdata->u.ap.ssid;
  604. conf.ssid_len = sdata->u.ap.ssid_len;
  605. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  606. u8 zero[ETH_ALEN] = { 0 };
  607. conf.bssid = zero;
  608. conf.ssid = zero;
  609. conf.ssid_len = 0;
  610. } else {
  611. WARN_ON(1);
  612. return -EINVAL;
  613. }
  614. if (WARN_ON(!conf.bssid && (changed & IEEE80211_IFCC_BSSID)))
  615. return -EINVAL;
  616. if (WARN_ON(!conf.ssid && (changed & IEEE80211_IFCC_SSID)))
  617. return -EINVAL;
  618. return local->ops->config_interface(local_to_hw(local),
  619. &sdata->vif, &conf);
  620. }
  621. int ieee80211_hw_config(struct ieee80211_local *local)
  622. {
  623. struct ieee80211_channel *chan;
  624. int ret = 0;
  625. if (local->sw_scanning)
  626. chan = local->scan_channel;
  627. else
  628. chan = local->oper_channel;
  629. local->hw.conf.channel = chan;
  630. if (!local->hw.conf.power_level)
  631. local->hw.conf.power_level = chan->max_power;
  632. else
  633. local->hw.conf.power_level = min(chan->max_power,
  634. local->hw.conf.power_level);
  635. local->hw.conf.max_antenna_gain = chan->max_antenna_gain;
  636. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  637. printk(KERN_DEBUG "%s: HW CONFIG: freq=%d\n",
  638. wiphy_name(local->hw.wiphy), chan->center_freq);
  639. #endif
  640. if (local->open_count)
  641. ret = local->ops->config(local_to_hw(local), &local->hw.conf);
  642. return ret;
  643. }
  644. /**
  645. * ieee80211_handle_ht should be used only after legacy configuration
  646. * has been determined namely band, as ht configuration depends upon
  647. * the hardware's HT abilities for a _specific_ band.
  648. */
  649. u32 ieee80211_handle_ht(struct ieee80211_local *local, int enable_ht,
  650. struct ieee80211_ht_info *req_ht_cap,
  651. struct ieee80211_ht_bss_info *req_bss_cap)
  652. {
  653. struct ieee80211_conf *conf = &local->hw.conf;
  654. struct ieee80211_supported_band *sband;
  655. struct ieee80211_ht_info ht_conf;
  656. struct ieee80211_ht_bss_info ht_bss_conf;
  657. u32 changed = 0;
  658. int i;
  659. u8 max_tx_streams = IEEE80211_HT_CAP_MAX_STREAMS;
  660. u8 tx_mcs_set_cap;
  661. sband = local->hw.wiphy->bands[conf->channel->band];
  662. memset(&ht_conf, 0, sizeof(struct ieee80211_ht_info));
  663. memset(&ht_bss_conf, 0, sizeof(struct ieee80211_ht_bss_info));
  664. /* HT is not supported */
  665. if (!sband->ht_info.ht_supported) {
  666. conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
  667. goto out;
  668. }
  669. /* disable HT */
  670. if (!enable_ht) {
  671. if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)
  672. changed |= BSS_CHANGED_HT;
  673. conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
  674. conf->ht_conf.ht_supported = 0;
  675. goto out;
  676. }
  677. if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE))
  678. changed |= BSS_CHANGED_HT;
  679. conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
  680. ht_conf.ht_supported = 1;
  681. ht_conf.cap = req_ht_cap->cap & sband->ht_info.cap;
  682. ht_conf.cap &= ~(IEEE80211_HT_CAP_SM_PS);
  683. ht_conf.cap |= sband->ht_info.cap & IEEE80211_HT_CAP_SM_PS;
  684. ht_bss_conf.primary_channel = req_bss_cap->primary_channel;
  685. ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
  686. ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
  687. ht_conf.ampdu_factor = req_ht_cap->ampdu_factor;
  688. ht_conf.ampdu_density = req_ht_cap->ampdu_density;
  689. /* Bits 96-100 */
  690. tx_mcs_set_cap = sband->ht_info.supp_mcs_set[12];
  691. /* configure suppoerted Tx MCS according to requested MCS
  692. * (based in most cases on Rx capabilities of peer) and self
  693. * Tx MCS capabilities (as defined by low level driver HW
  694. * Tx capabilities) */
  695. if (!(tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_DEFINED))
  696. goto check_changed;
  697. /* Counting from 0 therfore + 1 */
  698. if (tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_RX_DIFF)
  699. max_tx_streams = ((tx_mcs_set_cap &
  700. IEEE80211_HT_CAP_MCS_TX_STREAMS) >> 2) + 1;
  701. for (i = 0; i < max_tx_streams; i++)
  702. ht_conf.supp_mcs_set[i] =
  703. sband->ht_info.supp_mcs_set[i] &
  704. req_ht_cap->supp_mcs_set[i];
  705. if (tx_mcs_set_cap & IEEE80211_HT_CAP_MCS_TX_UEQM)
  706. for (i = IEEE80211_SUPP_MCS_SET_UEQM;
  707. i < IEEE80211_SUPP_MCS_SET_LEN; i++)
  708. ht_conf.supp_mcs_set[i] =
  709. sband->ht_info.supp_mcs_set[i] &
  710. req_ht_cap->supp_mcs_set[i];
  711. check_changed:
  712. /* if bss configuration changed store the new one */
  713. if (memcmp(&conf->ht_conf, &ht_conf, sizeof(ht_conf)) ||
  714. memcmp(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf))) {
  715. changed |= BSS_CHANGED_HT;
  716. memcpy(&conf->ht_conf, &ht_conf, sizeof(ht_conf));
  717. memcpy(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf));
  718. }
  719. out:
  720. return changed;
  721. }
  722. void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
  723. u32 changed)
  724. {
  725. struct ieee80211_local *local = sdata->local;
  726. if (!changed)
  727. return;
  728. if (local->ops->bss_info_changed)
  729. local->ops->bss_info_changed(local_to_hw(local),
  730. &sdata->vif,
  731. &sdata->bss_conf,
  732. changed);
  733. }
  734. u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
  735. {
  736. sdata->bss_conf.use_cts_prot = 0;
  737. sdata->bss_conf.use_short_preamble = 0;
  738. return BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_ERP_PREAMBLE;
  739. }
  740. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  741. struct sk_buff *skb)
  742. {
  743. struct ieee80211_local *local = hw_to_local(hw);
  744. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  745. int tmp;
  746. skb->dev = local->mdev;
  747. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  748. skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
  749. &local->skb_queue : &local->skb_queue_unreliable, skb);
  750. tmp = skb_queue_len(&local->skb_queue) +
  751. skb_queue_len(&local->skb_queue_unreliable);
  752. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  753. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  754. dev_kfree_skb_irq(skb);
  755. tmp--;
  756. I802_DEBUG_INC(local->tx_status_drop);
  757. }
  758. tasklet_schedule(&local->tasklet);
  759. }
  760. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  761. static void ieee80211_tasklet_handler(unsigned long data)
  762. {
  763. struct ieee80211_local *local = (struct ieee80211_local *) data;
  764. struct sk_buff *skb;
  765. struct ieee80211_rx_status rx_status;
  766. struct ieee80211_ra_tid *ra_tid;
  767. while ((skb = skb_dequeue(&local->skb_queue)) ||
  768. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  769. switch (skb->pkt_type) {
  770. case IEEE80211_RX_MSG:
  771. /* status is in skb->cb */
  772. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  773. /* Clear skb->pkt_type in order to not confuse kernel
  774. * netstack. */
  775. skb->pkt_type = 0;
  776. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  777. break;
  778. case IEEE80211_TX_STATUS_MSG:
  779. skb->pkt_type = 0;
  780. ieee80211_tx_status(local_to_hw(local), skb);
  781. break;
  782. case IEEE80211_DELBA_MSG:
  783. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  784. ieee80211_stop_tx_ba_cb(local_to_hw(local),
  785. ra_tid->ra, ra_tid->tid);
  786. dev_kfree_skb(skb);
  787. break;
  788. case IEEE80211_ADDBA_MSG:
  789. ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
  790. ieee80211_start_tx_ba_cb(local_to_hw(local),
  791. ra_tid->ra, ra_tid->tid);
  792. dev_kfree_skb(skb);
  793. break ;
  794. default:
  795. WARN_ON(1);
  796. dev_kfree_skb(skb);
  797. break;
  798. }
  799. }
  800. }
  801. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  802. * make a prepared TX frame (one that has been given to hw) to look like brand
  803. * new IEEE 802.11 frame that is ready to go through TX processing again.
  804. */
  805. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  806. struct ieee80211_key *key,
  807. struct sk_buff *skb)
  808. {
  809. unsigned int hdrlen, iv_len, mic_len;
  810. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  811. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  812. if (!key)
  813. goto no_key;
  814. switch (key->conf.alg) {
  815. case ALG_WEP:
  816. iv_len = WEP_IV_LEN;
  817. mic_len = WEP_ICV_LEN;
  818. break;
  819. case ALG_TKIP:
  820. iv_len = TKIP_IV_LEN;
  821. mic_len = TKIP_ICV_LEN;
  822. break;
  823. case ALG_CCMP:
  824. iv_len = CCMP_HDR_LEN;
  825. mic_len = CCMP_MIC_LEN;
  826. break;
  827. default:
  828. goto no_key;
  829. }
  830. if (skb->len >= hdrlen + mic_len &&
  831. !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  832. skb_trim(skb, skb->len - mic_len);
  833. if (skb->len >= hdrlen + iv_len) {
  834. memmove(skb->data + iv_len, skb->data, hdrlen);
  835. hdr = (struct ieee80211_hdr *)skb_pull(skb, iv_len);
  836. }
  837. no_key:
  838. if (ieee80211_is_data_qos(hdr->frame_control)) {
  839. hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  840. memmove(skb->data + IEEE80211_QOS_CTL_LEN, skb->data,
  841. hdrlen - IEEE80211_QOS_CTL_LEN);
  842. skb_pull(skb, IEEE80211_QOS_CTL_LEN);
  843. }
  844. }
  845. static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
  846. struct sta_info *sta,
  847. struct sk_buff *skb)
  848. {
  849. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  850. sta->tx_filtered_count++;
  851. /*
  852. * Clear the TX filter mask for this STA when sending the next
  853. * packet. If the STA went to power save mode, this will happen
  854. * when it wakes up for the next time.
  855. */
  856. set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
  857. /*
  858. * This code races in the following way:
  859. *
  860. * (1) STA sends frame indicating it will go to sleep and does so
  861. * (2) hardware/firmware adds STA to filter list, passes frame up
  862. * (3) hardware/firmware processes TX fifo and suppresses a frame
  863. * (4) we get TX status before having processed the frame and
  864. * knowing that the STA has gone to sleep.
  865. *
  866. * This is actually quite unlikely even when both those events are
  867. * processed from interrupts coming in quickly after one another or
  868. * even at the same time because we queue both TX status events and
  869. * RX frames to be processed by a tasklet and process them in the
  870. * same order that they were received or TX status last. Hence, there
  871. * is no race as long as the frame RX is processed before the next TX
  872. * status, which drivers can ensure, see below.
  873. *
  874. * Note that this can only happen if the hardware or firmware can
  875. * actually add STAs to the filter list, if this is done by the
  876. * driver in response to set_tim() (which will only reduce the race
  877. * this whole filtering tries to solve, not completely solve it)
  878. * this situation cannot happen.
  879. *
  880. * To completely solve this race drivers need to make sure that they
  881. * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
  882. * functions and
  883. * (b) always process RX events before TX status events if ordering
  884. * can be unknown, for example with different interrupt status
  885. * bits.
  886. */
  887. if (test_sta_flags(sta, WLAN_STA_PS) &&
  888. skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
  889. ieee80211_remove_tx_extra(local, sta->key, skb);
  890. skb_queue_tail(&sta->tx_filtered, skb);
  891. return;
  892. }
  893. if (!test_sta_flags(sta, WLAN_STA_PS) &&
  894. !(info->flags & IEEE80211_TX_CTL_REQUEUE)) {
  895. /* Software retry the packet once */
  896. info->flags |= IEEE80211_TX_CTL_REQUEUE;
  897. ieee80211_remove_tx_extra(local, sta->key, skb);
  898. dev_queue_xmit(skb);
  899. return;
  900. }
  901. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  902. if (net_ratelimit())
  903. printk(KERN_DEBUG "%s: dropped TX filtered frame, "
  904. "queue_len=%d PS=%d @%lu\n",
  905. wiphy_name(local->hw.wiphy),
  906. skb_queue_len(&sta->tx_filtered),
  907. !!test_sta_flags(sta, WLAN_STA_PS), jiffies);
  908. #endif
  909. dev_kfree_skb(skb);
  910. }
  911. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  912. {
  913. struct sk_buff *skb2;
  914. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  915. struct ieee80211_local *local = hw_to_local(hw);
  916. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  917. u16 frag, type;
  918. __le16 fc;
  919. struct ieee80211_tx_status_rtap_hdr *rthdr;
  920. struct ieee80211_sub_if_data *sdata;
  921. struct net_device *prev_dev = NULL;
  922. struct sta_info *sta;
  923. rcu_read_lock();
  924. if (info->status.excessive_retries) {
  925. sta = sta_info_get(local, hdr->addr1);
  926. if (sta) {
  927. if (test_sta_flags(sta, WLAN_STA_PS)) {
  928. /*
  929. * The STA is in power save mode, so assume
  930. * that this TX packet failed because of that.
  931. */
  932. ieee80211_handle_filtered_frame(local, sta, skb);
  933. rcu_read_unlock();
  934. return;
  935. }
  936. }
  937. }
  938. fc = hdr->frame_control;
  939. if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
  940. (ieee80211_is_data_qos(fc))) {
  941. u16 tid, ssn;
  942. u8 *qc;
  943. sta = sta_info_get(local, hdr->addr1);
  944. if (sta) {
  945. qc = ieee80211_get_qos_ctl(hdr);
  946. tid = qc[0] & 0xf;
  947. ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
  948. & IEEE80211_SCTL_SEQ);
  949. ieee80211_send_bar(sta->sdata, hdr->addr1,
  950. tid, ssn);
  951. }
  952. }
  953. if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  954. sta = sta_info_get(local, hdr->addr1);
  955. if (sta) {
  956. ieee80211_handle_filtered_frame(local, sta, skb);
  957. rcu_read_unlock();
  958. return;
  959. }
  960. } else
  961. rate_control_tx_status(local->mdev, skb);
  962. rcu_read_unlock();
  963. ieee80211_led_tx(local, 0);
  964. /* SNMP counters
  965. * Fragments are passed to low-level drivers as separate skbs, so these
  966. * are actually fragments, not frames. Update frame counters only for
  967. * the first fragment of the frame. */
  968. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  969. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  970. if (info->flags & IEEE80211_TX_STAT_ACK) {
  971. if (frag == 0) {
  972. local->dot11TransmittedFrameCount++;
  973. if (is_multicast_ether_addr(hdr->addr1))
  974. local->dot11MulticastTransmittedFrameCount++;
  975. if (info->status.retry_count > 0)
  976. local->dot11RetryCount++;
  977. if (info->status.retry_count > 1)
  978. local->dot11MultipleRetryCount++;
  979. }
  980. /* This counter shall be incremented for an acknowledged MPDU
  981. * with an individual address in the address 1 field or an MPDU
  982. * with a multicast address in the address 1 field of type Data
  983. * or Management. */
  984. if (!is_multicast_ether_addr(hdr->addr1) ||
  985. type == IEEE80211_FTYPE_DATA ||
  986. type == IEEE80211_FTYPE_MGMT)
  987. local->dot11TransmittedFragmentCount++;
  988. } else {
  989. if (frag == 0)
  990. local->dot11FailedCount++;
  991. }
  992. /* this was a transmitted frame, but now we want to reuse it */
  993. skb_orphan(skb);
  994. /*
  995. * This is a bit racy but we can avoid a lot of work
  996. * with this test...
  997. */
  998. if (!local->monitors && !local->cooked_mntrs) {
  999. dev_kfree_skb(skb);
  1000. return;
  1001. }
  1002. /* send frame to monitor interfaces now */
  1003. if (skb_headroom(skb) < sizeof(*rthdr)) {
  1004. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  1005. dev_kfree_skb(skb);
  1006. return;
  1007. }
  1008. rthdr = (struct ieee80211_tx_status_rtap_hdr *)
  1009. skb_push(skb, sizeof(*rthdr));
  1010. memset(rthdr, 0, sizeof(*rthdr));
  1011. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  1012. rthdr->hdr.it_present =
  1013. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  1014. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  1015. if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
  1016. !is_multicast_ether_addr(hdr->addr1))
  1017. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  1018. if ((info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) &&
  1019. (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT))
  1020. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  1021. else if (info->flags & IEEE80211_TX_CTL_USE_RTS_CTS)
  1022. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  1023. rthdr->data_retries = info->status.retry_count;
  1024. /* XXX: is this sufficient for BPF? */
  1025. skb_set_mac_header(skb, 0);
  1026. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1027. skb->pkt_type = PACKET_OTHERHOST;
  1028. skb->protocol = htons(ETH_P_802_2);
  1029. memset(skb->cb, 0, sizeof(skb->cb));
  1030. rcu_read_lock();
  1031. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  1032. if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
  1033. if (!netif_running(sdata->dev))
  1034. continue;
  1035. if (prev_dev) {
  1036. skb2 = skb_clone(skb, GFP_ATOMIC);
  1037. if (skb2) {
  1038. skb2->dev = prev_dev;
  1039. netif_rx(skb2);
  1040. }
  1041. }
  1042. prev_dev = sdata->dev;
  1043. }
  1044. }
  1045. if (prev_dev) {
  1046. skb->dev = prev_dev;
  1047. netif_rx(skb);
  1048. skb = NULL;
  1049. }
  1050. rcu_read_unlock();
  1051. dev_kfree_skb(skb);
  1052. }
  1053. EXPORT_SYMBOL(ieee80211_tx_status);
  1054. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  1055. const struct ieee80211_ops *ops)
  1056. {
  1057. struct ieee80211_local *local;
  1058. int priv_size;
  1059. struct wiphy *wiphy;
  1060. /* Ensure 32-byte alignment of our private data and hw private data.
  1061. * We use the wiphy priv data for both our ieee80211_local and for
  1062. * the driver's private data
  1063. *
  1064. * In memory it'll be like this:
  1065. *
  1066. * +-------------------------+
  1067. * | struct wiphy |
  1068. * +-------------------------+
  1069. * | struct ieee80211_local |
  1070. * +-------------------------+
  1071. * | driver's private data |
  1072. * +-------------------------+
  1073. *
  1074. */
  1075. priv_size = ((sizeof(struct ieee80211_local) +
  1076. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  1077. priv_data_len;
  1078. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  1079. if (!wiphy)
  1080. return NULL;
  1081. wiphy->privid = mac80211_wiphy_privid;
  1082. local = wiphy_priv(wiphy);
  1083. local->hw.wiphy = wiphy;
  1084. local->hw.priv = (char *)local +
  1085. ((sizeof(struct ieee80211_local) +
  1086. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  1087. BUG_ON(!ops->tx);
  1088. BUG_ON(!ops->start);
  1089. BUG_ON(!ops->stop);
  1090. BUG_ON(!ops->config);
  1091. BUG_ON(!ops->add_interface);
  1092. BUG_ON(!ops->remove_interface);
  1093. BUG_ON(!ops->configure_filter);
  1094. local->ops = ops;
  1095. local->hw.queues = 1; /* default */
  1096. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  1097. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  1098. local->short_retry_limit = 7;
  1099. local->long_retry_limit = 4;
  1100. local->hw.conf.radio_enabled = 1;
  1101. INIT_LIST_HEAD(&local->interfaces);
  1102. spin_lock_init(&local->key_lock);
  1103. INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
  1104. sta_info_init(local);
  1105. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  1106. (unsigned long)local);
  1107. tasklet_disable(&local->tx_pending_tasklet);
  1108. tasklet_init(&local->tasklet,
  1109. ieee80211_tasklet_handler,
  1110. (unsigned long) local);
  1111. tasklet_disable(&local->tasklet);
  1112. skb_queue_head_init(&local->skb_queue);
  1113. skb_queue_head_init(&local->skb_queue_unreliable);
  1114. return local_to_hw(local);
  1115. }
  1116. EXPORT_SYMBOL(ieee80211_alloc_hw);
  1117. int ieee80211_register_hw(struct ieee80211_hw *hw)
  1118. {
  1119. struct ieee80211_local *local = hw_to_local(hw);
  1120. const char *name;
  1121. int result;
  1122. enum ieee80211_band band;
  1123. struct net_device *mdev;
  1124. struct wireless_dev *mwdev;
  1125. /*
  1126. * generic code guarantees at least one band,
  1127. * set this very early because much code assumes
  1128. * that hw.conf.channel is assigned
  1129. */
  1130. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1131. struct ieee80211_supported_band *sband;
  1132. sband = local->hw.wiphy->bands[band];
  1133. if (sband) {
  1134. /* init channel we're on */
  1135. local->hw.conf.channel =
  1136. local->oper_channel =
  1137. local->scan_channel = &sband->channels[0];
  1138. break;
  1139. }
  1140. }
  1141. /* if low-level driver supports AP, we also support VLAN */
  1142. if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
  1143. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
  1144. /* mac80211 always supports monitor */
  1145. local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  1146. result = wiphy_register(local->hw.wiphy);
  1147. if (result < 0)
  1148. return result;
  1149. /*
  1150. * We use the number of queues for feature tests (QoS, HT) internally
  1151. * so restrict them appropriately.
  1152. */
  1153. if (hw->queues > IEEE80211_MAX_QUEUES)
  1154. hw->queues = IEEE80211_MAX_QUEUES;
  1155. if (hw->ampdu_queues > IEEE80211_MAX_AMPDU_QUEUES)
  1156. hw->ampdu_queues = IEEE80211_MAX_AMPDU_QUEUES;
  1157. if (hw->queues < 4)
  1158. hw->ampdu_queues = 0;
  1159. mdev = alloc_netdev_mq(sizeof(struct wireless_dev),
  1160. "wmaster%d", ether_setup,
  1161. ieee80211_num_queues(hw));
  1162. if (!mdev)
  1163. goto fail_mdev_alloc;
  1164. mwdev = netdev_priv(mdev);
  1165. mdev->ieee80211_ptr = mwdev;
  1166. mwdev->wiphy = local->hw.wiphy;
  1167. local->mdev = mdev;
  1168. ieee80211_rx_bss_list_init(local);
  1169. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  1170. mdev->open = ieee80211_master_open;
  1171. mdev->stop = ieee80211_master_stop;
  1172. mdev->type = ARPHRD_IEEE80211;
  1173. mdev->header_ops = &ieee80211_header_ops;
  1174. mdev->set_multicast_list = ieee80211_master_set_multicast_list;
  1175. name = wiphy_dev(local->hw.wiphy)->driver->name;
  1176. local->hw.workqueue = create_freezeable_workqueue(name);
  1177. if (!local->hw.workqueue) {
  1178. result = -ENOMEM;
  1179. goto fail_workqueue;
  1180. }
  1181. /*
  1182. * The hardware needs headroom for sending the frame,
  1183. * and we need some headroom for passing the frame to monitor
  1184. * interfaces, but never both at the same time.
  1185. */
  1186. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  1187. sizeof(struct ieee80211_tx_status_rtap_hdr));
  1188. debugfs_hw_add(local);
  1189. if (local->hw.conf.beacon_int < 10)
  1190. local->hw.conf.beacon_int = 100;
  1191. if (local->hw.max_listen_interval == 0)
  1192. local->hw.max_listen_interval = 1;
  1193. local->hw.conf.listen_interval = local->hw.max_listen_interval;
  1194. local->wstats_flags |= local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
  1195. IEEE80211_HW_SIGNAL_DB |
  1196. IEEE80211_HW_SIGNAL_DBM) ?
  1197. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  1198. local->wstats_flags |= local->hw.flags & IEEE80211_HW_NOISE_DBM ?
  1199. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  1200. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  1201. local->wstats_flags |= IW_QUAL_DBM;
  1202. result = sta_info_start(local);
  1203. if (result < 0)
  1204. goto fail_sta_info;
  1205. rtnl_lock();
  1206. result = dev_alloc_name(local->mdev, local->mdev->name);
  1207. if (result < 0)
  1208. goto fail_dev;
  1209. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1210. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  1211. result = register_netdevice(local->mdev);
  1212. if (result < 0)
  1213. goto fail_dev;
  1214. result = ieee80211_init_rate_ctrl_alg(local,
  1215. hw->rate_control_algorithm);
  1216. if (result < 0) {
  1217. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  1218. "algorithm\n", wiphy_name(local->hw.wiphy));
  1219. goto fail_rate;
  1220. }
  1221. result = ieee80211_wep_init(local);
  1222. if (result < 0) {
  1223. printk(KERN_DEBUG "%s: Failed to initialize wep: %d\n",
  1224. wiphy_name(local->hw.wiphy), result);
  1225. goto fail_wep;
  1226. }
  1227. local->mdev->select_queue = ieee80211_select_queue;
  1228. /* add one default STA interface */
  1229. result = ieee80211_if_add(local, "wlan%d", NULL,
  1230. NL80211_IFTYPE_STATION, NULL);
  1231. if (result)
  1232. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  1233. wiphy_name(local->hw.wiphy));
  1234. rtnl_unlock();
  1235. ieee80211_led_init(local);
  1236. return 0;
  1237. fail_wep:
  1238. rate_control_deinitialize(local);
  1239. fail_rate:
  1240. unregister_netdevice(local->mdev);
  1241. local->mdev = NULL;
  1242. fail_dev:
  1243. rtnl_unlock();
  1244. sta_info_stop(local);
  1245. fail_sta_info:
  1246. debugfs_hw_del(local);
  1247. destroy_workqueue(local->hw.workqueue);
  1248. fail_workqueue:
  1249. if (local->mdev)
  1250. free_netdev(local->mdev);
  1251. fail_mdev_alloc:
  1252. wiphy_unregister(local->hw.wiphy);
  1253. return result;
  1254. }
  1255. EXPORT_SYMBOL(ieee80211_register_hw);
  1256. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  1257. {
  1258. struct ieee80211_local *local = hw_to_local(hw);
  1259. tasklet_kill(&local->tx_pending_tasklet);
  1260. tasklet_kill(&local->tasklet);
  1261. rtnl_lock();
  1262. /*
  1263. * At this point, interface list manipulations are fine
  1264. * because the driver cannot be handing us frames any
  1265. * more and the tasklet is killed.
  1266. */
  1267. /* First, we remove all virtual interfaces. */
  1268. ieee80211_remove_interfaces(local);
  1269. /* then, finally, remove the master interface */
  1270. unregister_netdevice(local->mdev);
  1271. rtnl_unlock();
  1272. ieee80211_rx_bss_list_deinit(local);
  1273. ieee80211_clear_tx_pending(local);
  1274. sta_info_stop(local);
  1275. rate_control_deinitialize(local);
  1276. debugfs_hw_del(local);
  1277. if (skb_queue_len(&local->skb_queue)
  1278. || skb_queue_len(&local->skb_queue_unreliable))
  1279. printk(KERN_WARNING "%s: skb_queue not empty\n",
  1280. wiphy_name(local->hw.wiphy));
  1281. skb_queue_purge(&local->skb_queue);
  1282. skb_queue_purge(&local->skb_queue_unreliable);
  1283. destroy_workqueue(local->hw.workqueue);
  1284. wiphy_unregister(local->hw.wiphy);
  1285. ieee80211_wep_free(local);
  1286. ieee80211_led_exit(local);
  1287. free_netdev(local->mdev);
  1288. }
  1289. EXPORT_SYMBOL(ieee80211_unregister_hw);
  1290. void ieee80211_free_hw(struct ieee80211_hw *hw)
  1291. {
  1292. struct ieee80211_local *local = hw_to_local(hw);
  1293. wiphy_free(local->hw.wiphy);
  1294. }
  1295. EXPORT_SYMBOL(ieee80211_free_hw);
  1296. static int __init ieee80211_init(void)
  1297. {
  1298. struct sk_buff *skb;
  1299. int ret;
  1300. BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
  1301. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
  1302. IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
  1303. ret = rc80211_pid_init();
  1304. if (ret)
  1305. return ret;
  1306. ieee80211_debugfs_netdev_init();
  1307. return 0;
  1308. }
  1309. static void __exit ieee80211_exit(void)
  1310. {
  1311. rc80211_pid_exit();
  1312. /*
  1313. * For key todo, it'll be empty by now but the work
  1314. * might still be scheduled.
  1315. */
  1316. flush_scheduled_work();
  1317. if (mesh_allocated)
  1318. ieee80211s_stop();
  1319. ieee80211_debugfs_netdev_exit();
  1320. }
  1321. subsys_initcall(ieee80211_init);
  1322. module_exit(ieee80211_exit);
  1323. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  1324. MODULE_LICENSE("GPL");