main.c 42 KB

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