iface.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  1. /*
  2. * Interface handling (except master interface)
  3. *
  4. * Copyright 2002-2005, Instant802 Networks, Inc.
  5. * Copyright 2005-2006, Devicescape Software, Inc.
  6. * Copyright (c) 2006 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/slab.h>
  14. #include <linux/kernel.h>
  15. #include <linux/if_arp.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/rtnetlink.h>
  18. #include <net/mac80211.h>
  19. #include <net/ieee80211_radiotap.h>
  20. #include "ieee80211_i.h"
  21. #include "sta_info.h"
  22. #include "debugfs_netdev.h"
  23. #include "mesh.h"
  24. #include "led.h"
  25. #include "driver-ops.h"
  26. #include "wme.h"
  27. #include "rate.h"
  28. /**
  29. * DOC: Interface list locking
  30. *
  31. * The interface list in each struct ieee80211_local is protected
  32. * three-fold:
  33. *
  34. * (1) modifications may only be done under the RTNL
  35. * (2) modifications and readers are protected against each other by
  36. * the iflist_mtx.
  37. * (3) modifications are done in an RCU manner so atomic readers
  38. * can traverse the list in RCU-safe blocks.
  39. *
  40. * As a consequence, reads (traversals) of the list can be protected
  41. * by either the RTNL, the iflist_mtx or RCU.
  42. */
  43. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  44. {
  45. int meshhdrlen;
  46. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  47. meshhdrlen = (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) ? 5 : 0;
  48. /* FIX: what would be proper limits for MTU?
  49. * This interface uses 802.3 frames. */
  50. if (new_mtu < 256 ||
  51. new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
  52. return -EINVAL;
  53. }
  54. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  55. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  56. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  57. dev->mtu = new_mtu;
  58. return 0;
  59. }
  60. static int ieee80211_change_mac(struct net_device *dev, void *addr)
  61. {
  62. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  63. struct sockaddr *sa = addr;
  64. int ret;
  65. if (ieee80211_sdata_running(sdata))
  66. return -EBUSY;
  67. ret = eth_mac_addr(dev, sa);
  68. if (ret == 0)
  69. memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
  70. return ret;
  71. }
  72. static inline int identical_mac_addr_allowed(int type1, int type2)
  73. {
  74. return type1 == NL80211_IFTYPE_MONITOR ||
  75. type2 == NL80211_IFTYPE_MONITOR ||
  76. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
  77. (type1 == NL80211_IFTYPE_WDS &&
  78. (type2 == NL80211_IFTYPE_WDS ||
  79. type2 == NL80211_IFTYPE_AP)) ||
  80. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
  81. (type1 == NL80211_IFTYPE_AP_VLAN &&
  82. (type2 == NL80211_IFTYPE_AP ||
  83. type2 == NL80211_IFTYPE_AP_VLAN));
  84. }
  85. static int ieee80211_check_concurrent_iface(struct ieee80211_sub_if_data *sdata,
  86. enum nl80211_iftype iftype)
  87. {
  88. struct ieee80211_local *local = sdata->local;
  89. struct ieee80211_sub_if_data *nsdata;
  90. struct net_device *dev = sdata->dev;
  91. ASSERT_RTNL();
  92. /* we hold the RTNL here so can safely walk the list */
  93. list_for_each_entry(nsdata, &local->interfaces, list) {
  94. struct net_device *ndev = nsdata->dev;
  95. if (ndev != dev && ieee80211_sdata_running(nsdata)) {
  96. /*
  97. * Allow only a single IBSS interface to be up at any
  98. * time. This is restricted because beacon distribution
  99. * cannot work properly if both are in the same IBSS.
  100. *
  101. * To remove this restriction we'd have to disallow them
  102. * from setting the same SSID on different IBSS interfaces
  103. * belonging to the same hardware. Then, however, we're
  104. * faced with having to adopt two different TSF timers...
  105. */
  106. if (iftype == NL80211_IFTYPE_ADHOC &&
  107. nsdata->vif.type == NL80211_IFTYPE_ADHOC)
  108. return -EBUSY;
  109. /*
  110. * The remaining checks are only performed for interfaces
  111. * with the same MAC address.
  112. */
  113. if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
  114. continue;
  115. /*
  116. * check whether it may have the same address
  117. */
  118. if (!identical_mac_addr_allowed(iftype,
  119. nsdata->vif.type))
  120. return -ENOTUNIQ;
  121. /*
  122. * can only add VLANs to enabled APs
  123. */
  124. if (iftype == NL80211_IFTYPE_AP_VLAN &&
  125. nsdata->vif.type == NL80211_IFTYPE_AP)
  126. sdata->bss = &nsdata->u.ap;
  127. }
  128. }
  129. return 0;
  130. }
  131. void ieee80211_adjust_monitor_flags(struct ieee80211_sub_if_data *sdata,
  132. const int offset)
  133. {
  134. struct ieee80211_local *local = sdata->local;
  135. u32 flags = sdata->u.mntr_flags;
  136. #define ADJUST(_f, _s) do { \
  137. if (flags & MONITOR_FLAG_##_f) \
  138. local->fif_##_s += offset; \
  139. } while (0)
  140. ADJUST(FCSFAIL, fcsfail);
  141. ADJUST(PLCPFAIL, plcpfail);
  142. ADJUST(CONTROL, control);
  143. ADJUST(CONTROL, pspoll);
  144. ADJUST(OTHER_BSS, other_bss);
  145. #undef ADJUST
  146. }
  147. /*
  148. * NOTE: Be very careful when changing this function, it must NOT return
  149. * an error on interface type changes that have been pre-checked, so most
  150. * checks should be in ieee80211_check_concurrent_iface.
  151. */
  152. static int ieee80211_do_open(struct net_device *dev, bool coming_up)
  153. {
  154. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  155. struct ieee80211_local *local = sdata->local;
  156. struct sta_info *sta;
  157. u32 changed = 0;
  158. int res;
  159. u32 hw_reconf_flags = 0;
  160. switch (sdata->vif.type) {
  161. case NL80211_IFTYPE_WDS:
  162. if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
  163. return -ENOLINK;
  164. break;
  165. case NL80211_IFTYPE_AP_VLAN: {
  166. struct ieee80211_sub_if_data *master;
  167. if (!sdata->bss)
  168. return -ENOLINK;
  169. list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
  170. master = container_of(sdata->bss,
  171. struct ieee80211_sub_if_data, u.ap);
  172. sdata->control_port_protocol =
  173. master->control_port_protocol;
  174. sdata->control_port_no_encrypt =
  175. master->control_port_no_encrypt;
  176. break;
  177. }
  178. case NL80211_IFTYPE_AP:
  179. sdata->bss = &sdata->u.ap;
  180. break;
  181. case NL80211_IFTYPE_MESH_POINT:
  182. case NL80211_IFTYPE_STATION:
  183. case NL80211_IFTYPE_MONITOR:
  184. case NL80211_IFTYPE_ADHOC:
  185. /* no special treatment */
  186. break;
  187. case NL80211_IFTYPE_UNSPECIFIED:
  188. case NUM_NL80211_IFTYPES:
  189. case NL80211_IFTYPE_P2P_CLIENT:
  190. case NL80211_IFTYPE_P2P_GO:
  191. /* cannot happen */
  192. WARN_ON(1);
  193. break;
  194. }
  195. if (local->open_count == 0) {
  196. res = drv_start(local);
  197. if (res)
  198. goto err_del_bss;
  199. if (local->ops->napi_poll)
  200. napi_enable(&local->napi);
  201. /* we're brought up, everything changes */
  202. hw_reconf_flags = ~0;
  203. ieee80211_led_radio(local, true);
  204. ieee80211_mod_tpt_led_trig(local,
  205. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  206. }
  207. /*
  208. * Copy the hopefully now-present MAC address to
  209. * this interface, if it has the special null one.
  210. */
  211. if (is_zero_ether_addr(dev->dev_addr)) {
  212. memcpy(dev->dev_addr,
  213. local->hw.wiphy->perm_addr,
  214. ETH_ALEN);
  215. memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
  216. if (!is_valid_ether_addr(dev->dev_addr)) {
  217. if (!local->open_count)
  218. drv_stop(local);
  219. return -EADDRNOTAVAIL;
  220. }
  221. }
  222. switch (sdata->vif.type) {
  223. case NL80211_IFTYPE_AP_VLAN:
  224. /* no need to tell driver */
  225. break;
  226. case NL80211_IFTYPE_MONITOR:
  227. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  228. local->cooked_mntrs++;
  229. break;
  230. }
  231. /* must be before the call to ieee80211_configure_filter */
  232. local->monitors++;
  233. if (local->monitors == 1) {
  234. local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
  235. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  236. }
  237. ieee80211_adjust_monitor_flags(sdata, 1);
  238. ieee80211_configure_filter(local);
  239. netif_carrier_on(dev);
  240. break;
  241. default:
  242. if (coming_up) {
  243. res = drv_add_interface(local, sdata);
  244. if (res)
  245. goto err_stop;
  246. }
  247. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  248. local->fif_pspoll++;
  249. local->fif_probe_req++;
  250. ieee80211_configure_filter(local);
  251. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  252. local->fif_probe_req++;
  253. }
  254. changed |= ieee80211_reset_erp_info(sdata);
  255. ieee80211_bss_info_change_notify(sdata, changed);
  256. if (sdata->vif.type == NL80211_IFTYPE_STATION ||
  257. sdata->vif.type == NL80211_IFTYPE_ADHOC)
  258. netif_carrier_off(dev);
  259. else
  260. netif_carrier_on(dev);
  261. /*
  262. * set default queue parameters so drivers don't
  263. * need to initialise the hardware if the hardware
  264. * doesn't start up with sane defaults
  265. */
  266. ieee80211_set_wmm_default(sdata, true);
  267. }
  268. set_bit(SDATA_STATE_RUNNING, &sdata->state);
  269. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  270. /* Create STA entry for the WDS peer */
  271. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  272. GFP_KERNEL);
  273. if (!sta) {
  274. res = -ENOMEM;
  275. goto err_del_interface;
  276. }
  277. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  278. sta_info_pre_move_state(sta, IEEE80211_STA_ASSOC);
  279. sta_info_pre_move_state(sta, IEEE80211_STA_AUTHORIZED);
  280. res = sta_info_insert(sta);
  281. if (res) {
  282. /* STA has been freed */
  283. goto err_del_interface;
  284. }
  285. rate_control_rate_init(sta);
  286. }
  287. /*
  288. * set_multicast_list will be invoked by the networking core
  289. * which will check whether any increments here were done in
  290. * error and sync them down to the hardware as filter flags.
  291. */
  292. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  293. atomic_inc(&local->iff_allmultis);
  294. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  295. atomic_inc(&local->iff_promiscs);
  296. mutex_lock(&local->mtx);
  297. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  298. mutex_unlock(&local->mtx);
  299. if (coming_up)
  300. local->open_count++;
  301. if (hw_reconf_flags)
  302. ieee80211_hw_config(local, hw_reconf_flags);
  303. ieee80211_recalc_ps(local, -1);
  304. netif_tx_start_all_queues(dev);
  305. return 0;
  306. err_del_interface:
  307. drv_remove_interface(local, sdata);
  308. err_stop:
  309. if (!local->open_count)
  310. drv_stop(local);
  311. err_del_bss:
  312. sdata->bss = NULL;
  313. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  314. list_del(&sdata->u.vlan.list);
  315. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  316. return res;
  317. }
  318. static int ieee80211_open(struct net_device *dev)
  319. {
  320. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  321. int err;
  322. /* fail early if user set an invalid address */
  323. if (!is_valid_ether_addr(dev->dev_addr))
  324. return -EADDRNOTAVAIL;
  325. err = ieee80211_check_concurrent_iface(sdata, sdata->vif.type);
  326. if (err)
  327. return err;
  328. return ieee80211_do_open(dev, true);
  329. }
  330. static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
  331. bool going_down)
  332. {
  333. struct ieee80211_local *local = sdata->local;
  334. unsigned long flags;
  335. struct sk_buff *skb, *tmp;
  336. u32 hw_reconf_flags = 0;
  337. int i;
  338. enum nl80211_channel_type orig_ct;
  339. bool cancel_scan;
  340. clear_bit(SDATA_STATE_RUNNING, &sdata->state);
  341. cancel_scan = local->scan_sdata == sdata;
  342. if (cancel_scan)
  343. ieee80211_scan_cancel(local);
  344. /*
  345. * Stop TX on this interface first.
  346. */
  347. netif_tx_stop_all_queues(sdata->dev);
  348. /*
  349. * Purge work for this interface.
  350. */
  351. ieee80211_work_purge(sdata);
  352. /*
  353. * Remove all stations associated with this interface.
  354. *
  355. * This must be done before calling ops->remove_interface()
  356. * because otherwise we can later invoke ops->sta_notify()
  357. * whenever the STAs are removed, and that invalidates driver
  358. * assumptions about always getting a vif pointer that is valid
  359. * (because if we remove a STA after ops->remove_interface()
  360. * the driver will have removed the vif info already!)
  361. *
  362. * This is relevant only in AP, WDS and mesh modes, since in
  363. * all other modes we've already removed all stations when
  364. * disconnecting etc.
  365. */
  366. sta_info_flush(local, sdata);
  367. /*
  368. * Don't count this interface for promisc/allmulti while it
  369. * is down. dev_mc_unsync() will invoke set_multicast_list
  370. * on the master interface which will sync these down to the
  371. * hardware as filter flags.
  372. */
  373. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  374. atomic_dec(&local->iff_allmultis);
  375. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  376. atomic_dec(&local->iff_promiscs);
  377. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  378. local->fif_pspoll--;
  379. local->fif_probe_req--;
  380. } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  381. local->fif_probe_req--;
  382. }
  383. netif_addr_lock_bh(sdata->dev);
  384. spin_lock_bh(&local->filter_lock);
  385. __hw_addr_unsync(&local->mc_list, &sdata->dev->mc,
  386. sdata->dev->addr_len);
  387. spin_unlock_bh(&local->filter_lock);
  388. netif_addr_unlock_bh(sdata->dev);
  389. ieee80211_configure_filter(local);
  390. del_timer_sync(&local->dynamic_ps_timer);
  391. cancel_work_sync(&local->dynamic_ps_enable_work);
  392. /* APs need special treatment */
  393. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  394. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  395. struct beacon_data *old_beacon =
  396. rtnl_dereference(sdata->u.ap.beacon);
  397. struct sk_buff *old_probe_resp =
  398. rtnl_dereference(sdata->u.ap.probe_resp);
  399. /* sdata_running will return false, so this will disable */
  400. ieee80211_bss_info_change_notify(sdata,
  401. BSS_CHANGED_BEACON_ENABLED);
  402. /* remove beacon and probe response */
  403. RCU_INIT_POINTER(sdata->u.ap.beacon, NULL);
  404. RCU_INIT_POINTER(sdata->u.ap.probe_resp, NULL);
  405. synchronize_rcu();
  406. kfree(old_beacon);
  407. kfree_skb(old_probe_resp);
  408. /* down all dependent devices, that is VLANs */
  409. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  410. u.vlan.list)
  411. dev_close(vlan->dev);
  412. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  413. /* free all potentially still buffered bcast frames */
  414. local->total_ps_buffered -= skb_queue_len(&sdata->u.ap.ps_bc_buf);
  415. skb_queue_purge(&sdata->u.ap.ps_bc_buf);
  416. } else if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  417. ieee80211_mgd_stop(sdata);
  418. }
  419. if (going_down)
  420. local->open_count--;
  421. switch (sdata->vif.type) {
  422. case NL80211_IFTYPE_AP_VLAN:
  423. list_del(&sdata->u.vlan.list);
  424. /* no need to tell driver */
  425. break;
  426. case NL80211_IFTYPE_MONITOR:
  427. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  428. local->cooked_mntrs--;
  429. break;
  430. }
  431. local->monitors--;
  432. if (local->monitors == 0) {
  433. local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
  434. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  435. }
  436. ieee80211_adjust_monitor_flags(sdata, -1);
  437. ieee80211_configure_filter(local);
  438. break;
  439. default:
  440. mutex_lock(&local->mtx);
  441. if (local->hw_roc_dev == sdata->dev &&
  442. local->hw_roc_channel) {
  443. /* ignore return value since this is racy */
  444. drv_cancel_remain_on_channel(local);
  445. ieee80211_queue_work(&local->hw, &local->hw_roc_done);
  446. }
  447. mutex_unlock(&local->mtx);
  448. flush_work(&local->hw_roc_start);
  449. flush_work(&local->hw_roc_done);
  450. flush_work(&sdata->work);
  451. /*
  452. * When we get here, the interface is marked down.
  453. * Call synchronize_rcu() to wait for the RX path
  454. * should it be using the interface and enqueuing
  455. * frames at this very time on another CPU.
  456. */
  457. synchronize_rcu();
  458. skb_queue_purge(&sdata->skb_queue);
  459. /*
  460. * Disable beaconing here for mesh only, AP and IBSS
  461. * are already taken care of.
  462. */
  463. if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  464. ieee80211_bss_info_change_notify(sdata,
  465. BSS_CHANGED_BEACON_ENABLED);
  466. /*
  467. * Free all remaining keys, there shouldn't be any,
  468. * except maybe group keys in AP more or WDS?
  469. */
  470. ieee80211_free_keys(sdata);
  471. if (going_down)
  472. drv_remove_interface(local, sdata);
  473. }
  474. sdata->bss = NULL;
  475. mutex_lock(&local->mtx);
  476. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  477. mutex_unlock(&local->mtx);
  478. ieee80211_recalc_ps(local, -1);
  479. if (cancel_scan)
  480. flush_delayed_work(&local->scan_work);
  481. if (local->open_count == 0) {
  482. if (local->ops->napi_poll)
  483. napi_disable(&local->napi);
  484. ieee80211_clear_tx_pending(local);
  485. ieee80211_stop_device(local);
  486. /* no reconfiguring after stop! */
  487. hw_reconf_flags = 0;
  488. }
  489. /* Re-calculate channel-type, in case there are multiple vifs
  490. * on different channel types.
  491. */
  492. orig_ct = local->_oper_channel_type;
  493. ieee80211_set_channel_type(local, NULL, NL80211_CHAN_NO_HT);
  494. /* do after stop to avoid reconfiguring when we stop anyway */
  495. if (hw_reconf_flags || (orig_ct != local->_oper_channel_type))
  496. ieee80211_hw_config(local, hw_reconf_flags);
  497. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  498. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  499. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  500. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  501. if (info->control.vif == &sdata->vif) {
  502. __skb_unlink(skb, &local->pending[i]);
  503. dev_kfree_skb_irq(skb);
  504. }
  505. }
  506. }
  507. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  508. }
  509. static int ieee80211_stop(struct net_device *dev)
  510. {
  511. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  512. ieee80211_do_stop(sdata, true);
  513. return 0;
  514. }
  515. static void ieee80211_set_multicast_list(struct net_device *dev)
  516. {
  517. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  518. struct ieee80211_local *local = sdata->local;
  519. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  520. allmulti = !!(dev->flags & IFF_ALLMULTI);
  521. promisc = !!(dev->flags & IFF_PROMISC);
  522. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  523. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  524. if (allmulti != sdata_allmulti) {
  525. if (dev->flags & IFF_ALLMULTI)
  526. atomic_inc(&local->iff_allmultis);
  527. else
  528. atomic_dec(&local->iff_allmultis);
  529. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  530. }
  531. if (promisc != sdata_promisc) {
  532. if (dev->flags & IFF_PROMISC)
  533. atomic_inc(&local->iff_promiscs);
  534. else
  535. atomic_dec(&local->iff_promiscs);
  536. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  537. }
  538. spin_lock_bh(&local->filter_lock);
  539. __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
  540. spin_unlock_bh(&local->filter_lock);
  541. ieee80211_queue_work(&local->hw, &local->reconfig_filter);
  542. }
  543. /*
  544. * Called when the netdev is removed or, by the code below, before
  545. * the interface type changes.
  546. */
  547. static void ieee80211_teardown_sdata(struct net_device *dev)
  548. {
  549. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  550. struct ieee80211_local *local = sdata->local;
  551. int flushed;
  552. int i;
  553. /* free extra data */
  554. ieee80211_free_keys(sdata);
  555. ieee80211_debugfs_remove_netdev(sdata);
  556. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  557. __skb_queue_purge(&sdata->fragments[i].skb_list);
  558. sdata->fragment_next = 0;
  559. if (ieee80211_vif_is_mesh(&sdata->vif))
  560. mesh_rmc_free(sdata);
  561. flushed = sta_info_flush(local, sdata);
  562. WARN_ON(flushed);
  563. }
  564. static u16 ieee80211_netdev_select_queue(struct net_device *dev,
  565. struct sk_buff *skb)
  566. {
  567. return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
  568. }
  569. static const struct net_device_ops ieee80211_dataif_ops = {
  570. .ndo_open = ieee80211_open,
  571. .ndo_stop = ieee80211_stop,
  572. .ndo_uninit = ieee80211_teardown_sdata,
  573. .ndo_start_xmit = ieee80211_subif_start_xmit,
  574. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  575. .ndo_change_mtu = ieee80211_change_mtu,
  576. .ndo_set_mac_address = ieee80211_change_mac,
  577. .ndo_select_queue = ieee80211_netdev_select_queue,
  578. };
  579. static u16 ieee80211_monitor_select_queue(struct net_device *dev,
  580. struct sk_buff *skb)
  581. {
  582. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  583. struct ieee80211_local *local = sdata->local;
  584. struct ieee80211_hdr *hdr;
  585. struct ieee80211_radiotap_header *rtap = (void *)skb->data;
  586. if (local->hw.queues < 4)
  587. return 0;
  588. if (skb->len < 4 ||
  589. skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
  590. return 0; /* doesn't matter, frame will be dropped */
  591. hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
  592. return ieee80211_select_queue_80211(local, skb, hdr);
  593. }
  594. static const struct net_device_ops ieee80211_monitorif_ops = {
  595. .ndo_open = ieee80211_open,
  596. .ndo_stop = ieee80211_stop,
  597. .ndo_uninit = ieee80211_teardown_sdata,
  598. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  599. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  600. .ndo_change_mtu = ieee80211_change_mtu,
  601. .ndo_set_mac_address = eth_mac_addr,
  602. .ndo_select_queue = ieee80211_monitor_select_queue,
  603. };
  604. static void ieee80211_if_setup(struct net_device *dev)
  605. {
  606. ether_setup(dev);
  607. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  608. dev->netdev_ops = &ieee80211_dataif_ops;
  609. dev->destructor = free_netdev;
  610. }
  611. static void ieee80211_iface_work(struct work_struct *work)
  612. {
  613. struct ieee80211_sub_if_data *sdata =
  614. container_of(work, struct ieee80211_sub_if_data, work);
  615. struct ieee80211_local *local = sdata->local;
  616. struct sk_buff *skb;
  617. struct sta_info *sta;
  618. struct ieee80211_ra_tid *ra_tid;
  619. if (!ieee80211_sdata_running(sdata))
  620. return;
  621. if (local->scanning)
  622. return;
  623. /*
  624. * ieee80211_queue_work() should have picked up most cases,
  625. * here we'll pick the rest.
  626. */
  627. if (WARN(local->suspended,
  628. "interface work scheduled while going to suspend\n"))
  629. return;
  630. /* first process frames */
  631. while ((skb = skb_dequeue(&sdata->skb_queue))) {
  632. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  633. if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_START) {
  634. ra_tid = (void *)&skb->cb;
  635. ieee80211_start_tx_ba_cb(&sdata->vif, ra_tid->ra,
  636. ra_tid->tid);
  637. } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_STOP) {
  638. ra_tid = (void *)&skb->cb;
  639. ieee80211_stop_tx_ba_cb(&sdata->vif, ra_tid->ra,
  640. ra_tid->tid);
  641. } else if (ieee80211_is_action(mgmt->frame_control) &&
  642. mgmt->u.action.category == WLAN_CATEGORY_BACK) {
  643. int len = skb->len;
  644. mutex_lock(&local->sta_mtx);
  645. sta = sta_info_get_bss(sdata, mgmt->sa);
  646. if (sta) {
  647. switch (mgmt->u.action.u.addba_req.action_code) {
  648. case WLAN_ACTION_ADDBA_REQ:
  649. ieee80211_process_addba_request(
  650. local, sta, mgmt, len);
  651. break;
  652. case WLAN_ACTION_ADDBA_RESP:
  653. ieee80211_process_addba_resp(local, sta,
  654. mgmt, len);
  655. break;
  656. case WLAN_ACTION_DELBA:
  657. ieee80211_process_delba(sdata, sta,
  658. mgmt, len);
  659. break;
  660. default:
  661. WARN_ON(1);
  662. break;
  663. }
  664. }
  665. mutex_unlock(&local->sta_mtx);
  666. } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
  667. struct ieee80211_hdr *hdr = (void *)mgmt;
  668. /*
  669. * So the frame isn't mgmt, but frame_control
  670. * is at the right place anyway, of course, so
  671. * the if statement is correct.
  672. *
  673. * Warn if we have other data frame types here,
  674. * they must not get here.
  675. */
  676. WARN_ON(hdr->frame_control &
  677. cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
  678. WARN_ON(!(hdr->seq_ctrl &
  679. cpu_to_le16(IEEE80211_SCTL_FRAG)));
  680. /*
  681. * This was a fragment of a frame, received while
  682. * a block-ack session was active. That cannot be
  683. * right, so terminate the session.
  684. */
  685. mutex_lock(&local->sta_mtx);
  686. sta = sta_info_get_bss(sdata, mgmt->sa);
  687. if (sta) {
  688. u16 tid = *ieee80211_get_qos_ctl(hdr) &
  689. IEEE80211_QOS_CTL_TID_MASK;
  690. __ieee80211_stop_rx_ba_session(
  691. sta, tid, WLAN_BACK_RECIPIENT,
  692. WLAN_REASON_QSTA_REQUIRE_SETUP,
  693. true);
  694. }
  695. mutex_unlock(&local->sta_mtx);
  696. } else switch (sdata->vif.type) {
  697. case NL80211_IFTYPE_STATION:
  698. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  699. break;
  700. case NL80211_IFTYPE_ADHOC:
  701. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  702. break;
  703. case NL80211_IFTYPE_MESH_POINT:
  704. if (!ieee80211_vif_is_mesh(&sdata->vif))
  705. break;
  706. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  707. break;
  708. default:
  709. WARN(1, "frame for unexpected interface type");
  710. break;
  711. }
  712. kfree_skb(skb);
  713. }
  714. /* then other type-dependent work */
  715. switch (sdata->vif.type) {
  716. case NL80211_IFTYPE_STATION:
  717. ieee80211_sta_work(sdata);
  718. break;
  719. case NL80211_IFTYPE_ADHOC:
  720. ieee80211_ibss_work(sdata);
  721. break;
  722. case NL80211_IFTYPE_MESH_POINT:
  723. if (!ieee80211_vif_is_mesh(&sdata->vif))
  724. break;
  725. ieee80211_mesh_work(sdata);
  726. break;
  727. default:
  728. break;
  729. }
  730. }
  731. /*
  732. * Helper function to initialise an interface to a specific type.
  733. */
  734. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  735. enum nl80211_iftype type)
  736. {
  737. /* clear type-dependent union */
  738. memset(&sdata->u, 0, sizeof(sdata->u));
  739. /* and set some type-dependent values */
  740. sdata->vif.type = type;
  741. sdata->vif.p2p = false;
  742. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  743. sdata->wdev.iftype = type;
  744. sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
  745. sdata->control_port_no_encrypt = false;
  746. sdata->noack_map = 0;
  747. /* only monitor differs */
  748. sdata->dev->type = ARPHRD_ETHER;
  749. skb_queue_head_init(&sdata->skb_queue);
  750. INIT_WORK(&sdata->work, ieee80211_iface_work);
  751. switch (type) {
  752. case NL80211_IFTYPE_P2P_GO:
  753. type = NL80211_IFTYPE_AP;
  754. sdata->vif.type = type;
  755. sdata->vif.p2p = true;
  756. /* fall through */
  757. case NL80211_IFTYPE_AP:
  758. skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
  759. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  760. break;
  761. case NL80211_IFTYPE_P2P_CLIENT:
  762. type = NL80211_IFTYPE_STATION;
  763. sdata->vif.type = type;
  764. sdata->vif.p2p = true;
  765. /* fall through */
  766. case NL80211_IFTYPE_STATION:
  767. ieee80211_sta_setup_sdata(sdata);
  768. break;
  769. case NL80211_IFTYPE_ADHOC:
  770. ieee80211_ibss_setup_sdata(sdata);
  771. break;
  772. case NL80211_IFTYPE_MESH_POINT:
  773. if (ieee80211_vif_is_mesh(&sdata->vif))
  774. ieee80211_mesh_init_sdata(sdata);
  775. break;
  776. case NL80211_IFTYPE_MONITOR:
  777. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  778. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  779. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  780. MONITOR_FLAG_OTHER_BSS;
  781. break;
  782. case NL80211_IFTYPE_WDS:
  783. case NL80211_IFTYPE_AP_VLAN:
  784. break;
  785. case NL80211_IFTYPE_UNSPECIFIED:
  786. case NUM_NL80211_IFTYPES:
  787. BUG();
  788. break;
  789. }
  790. ieee80211_debugfs_add_netdev(sdata);
  791. }
  792. static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
  793. enum nl80211_iftype type)
  794. {
  795. struct ieee80211_local *local = sdata->local;
  796. int ret, err;
  797. enum nl80211_iftype internal_type = type;
  798. bool p2p = false;
  799. ASSERT_RTNL();
  800. if (!local->ops->change_interface)
  801. return -EBUSY;
  802. switch (sdata->vif.type) {
  803. case NL80211_IFTYPE_AP:
  804. case NL80211_IFTYPE_STATION:
  805. case NL80211_IFTYPE_ADHOC:
  806. /*
  807. * Could maybe also all others here?
  808. * Just not sure how that interacts
  809. * with the RX/config path e.g. for
  810. * mesh.
  811. */
  812. break;
  813. default:
  814. return -EBUSY;
  815. }
  816. switch (type) {
  817. case NL80211_IFTYPE_AP:
  818. case NL80211_IFTYPE_STATION:
  819. case NL80211_IFTYPE_ADHOC:
  820. /*
  821. * Could probably support everything
  822. * but WDS here (WDS do_open can fail
  823. * under memory pressure, which this
  824. * code isn't prepared to handle).
  825. */
  826. break;
  827. case NL80211_IFTYPE_P2P_CLIENT:
  828. p2p = true;
  829. internal_type = NL80211_IFTYPE_STATION;
  830. break;
  831. case NL80211_IFTYPE_P2P_GO:
  832. p2p = true;
  833. internal_type = NL80211_IFTYPE_AP;
  834. break;
  835. default:
  836. return -EBUSY;
  837. }
  838. ret = ieee80211_check_concurrent_iface(sdata, internal_type);
  839. if (ret)
  840. return ret;
  841. ieee80211_do_stop(sdata, false);
  842. ieee80211_teardown_sdata(sdata->dev);
  843. ret = drv_change_interface(local, sdata, internal_type, p2p);
  844. if (ret)
  845. type = sdata->vif.type;
  846. ieee80211_setup_sdata(sdata, type);
  847. err = ieee80211_do_open(sdata->dev, false);
  848. WARN(err, "type change: do_open returned %d", err);
  849. return ret;
  850. }
  851. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  852. enum nl80211_iftype type)
  853. {
  854. int ret;
  855. ASSERT_RTNL();
  856. if (type == ieee80211_vif_type_p2p(&sdata->vif))
  857. return 0;
  858. /* Setting ad-hoc mode on non-IBSS channel is not supported. */
  859. if (sdata->local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS &&
  860. type == NL80211_IFTYPE_ADHOC)
  861. return -EOPNOTSUPP;
  862. if (ieee80211_sdata_running(sdata)) {
  863. ret = ieee80211_runtime_change_iftype(sdata, type);
  864. if (ret)
  865. return ret;
  866. } else {
  867. /* Purge and reset type-dependent state. */
  868. ieee80211_teardown_sdata(sdata->dev);
  869. ieee80211_setup_sdata(sdata, type);
  870. }
  871. /* reset some values that shouldn't be kept across type changes */
  872. sdata->vif.bss_conf.basic_rates =
  873. ieee80211_mandatory_rates(sdata->local,
  874. sdata->local->hw.conf.channel->band);
  875. sdata->drop_unencrypted = 0;
  876. if (type == NL80211_IFTYPE_STATION)
  877. sdata->u.mgd.use_4addr = false;
  878. return 0;
  879. }
  880. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  881. struct net_device *dev,
  882. enum nl80211_iftype type)
  883. {
  884. struct ieee80211_sub_if_data *sdata;
  885. u64 mask, start, addr, val, inc;
  886. u8 *m;
  887. u8 tmp_addr[ETH_ALEN];
  888. int i;
  889. /* default ... something at least */
  890. memcpy(dev->perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  891. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  892. local->hw.wiphy->n_addresses <= 1)
  893. return;
  894. mutex_lock(&local->iflist_mtx);
  895. switch (type) {
  896. case NL80211_IFTYPE_MONITOR:
  897. /* doesn't matter */
  898. break;
  899. case NL80211_IFTYPE_WDS:
  900. case NL80211_IFTYPE_AP_VLAN:
  901. /* match up with an AP interface */
  902. list_for_each_entry(sdata, &local->interfaces, list) {
  903. if (sdata->vif.type != NL80211_IFTYPE_AP)
  904. continue;
  905. memcpy(dev->perm_addr, sdata->vif.addr, ETH_ALEN);
  906. break;
  907. }
  908. /* keep default if no AP interface present */
  909. break;
  910. default:
  911. /* assign a new address if possible -- try n_addresses first */
  912. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  913. bool used = false;
  914. list_for_each_entry(sdata, &local->interfaces, list) {
  915. if (memcmp(local->hw.wiphy->addresses[i].addr,
  916. sdata->vif.addr, ETH_ALEN) == 0) {
  917. used = true;
  918. break;
  919. }
  920. }
  921. if (!used) {
  922. memcpy(dev->perm_addr,
  923. local->hw.wiphy->addresses[i].addr,
  924. ETH_ALEN);
  925. break;
  926. }
  927. }
  928. /* try mask if available */
  929. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  930. break;
  931. m = local->hw.wiphy->addr_mask;
  932. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  933. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  934. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  935. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  936. /* not a contiguous mask ... not handled now! */
  937. printk(KERN_DEBUG "not contiguous\n");
  938. break;
  939. }
  940. m = local->hw.wiphy->perm_addr;
  941. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  942. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  943. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  944. inc = 1ULL<<__ffs64(mask);
  945. val = (start & mask);
  946. addr = (start & ~mask) | (val & mask);
  947. do {
  948. bool used = false;
  949. tmp_addr[5] = addr >> 0*8;
  950. tmp_addr[4] = addr >> 1*8;
  951. tmp_addr[3] = addr >> 2*8;
  952. tmp_addr[2] = addr >> 3*8;
  953. tmp_addr[1] = addr >> 4*8;
  954. tmp_addr[0] = addr >> 5*8;
  955. val += inc;
  956. list_for_each_entry(sdata, &local->interfaces, list) {
  957. if (memcmp(tmp_addr, sdata->vif.addr,
  958. ETH_ALEN) == 0) {
  959. used = true;
  960. break;
  961. }
  962. }
  963. if (!used) {
  964. memcpy(dev->perm_addr, tmp_addr, ETH_ALEN);
  965. break;
  966. }
  967. addr = (start & ~mask) | (val & mask);
  968. } while (addr != start);
  969. break;
  970. }
  971. mutex_unlock(&local->iflist_mtx);
  972. }
  973. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  974. struct net_device **new_dev, enum nl80211_iftype type,
  975. struct vif_params *params)
  976. {
  977. struct net_device *ndev;
  978. struct ieee80211_sub_if_data *sdata = NULL;
  979. int ret, i;
  980. ASSERT_RTNL();
  981. ndev = alloc_netdev_mqs(sizeof(*sdata) + local->hw.vif_data_size,
  982. name, ieee80211_if_setup, local->hw.queues, 1);
  983. if (!ndev)
  984. return -ENOMEM;
  985. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  986. ndev->needed_headroom = local->tx_headroom +
  987. 4*6 /* four MAC addresses */
  988. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  989. + 6 /* mesh */
  990. + 8 /* rfc1042/bridge tunnel */
  991. - ETH_HLEN /* ethernet hard_header_len */
  992. + IEEE80211_ENCRYPT_HEADROOM;
  993. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  994. ret = dev_alloc_name(ndev, ndev->name);
  995. if (ret < 0)
  996. goto fail;
  997. ieee80211_assign_perm_addr(local, ndev, type);
  998. memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
  999. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  1000. /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
  1001. sdata = netdev_priv(ndev);
  1002. ndev->ieee80211_ptr = &sdata->wdev;
  1003. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  1004. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  1005. /* initialise type-independent data */
  1006. sdata->wdev.wiphy = local->hw.wiphy;
  1007. sdata->local = local;
  1008. sdata->dev = ndev;
  1009. #ifdef CONFIG_INET
  1010. sdata->arp_filter_state = true;
  1011. #endif
  1012. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  1013. skb_queue_head_init(&sdata->fragments[i].skb_list);
  1014. INIT_LIST_HEAD(&sdata->key_list);
  1015. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  1016. struct ieee80211_supported_band *sband;
  1017. sband = local->hw.wiphy->bands[i];
  1018. sdata->rc_rateidx_mask[i] =
  1019. sband ? (1 << sband->n_bitrates) - 1 : 0;
  1020. if (sband)
  1021. memcpy(sdata->rc_rateidx_mcs_mask[i],
  1022. sband->ht_cap.mcs.rx_mask,
  1023. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1024. else
  1025. memset(sdata->rc_rateidx_mcs_mask[i], 0,
  1026. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1027. }
  1028. /* setup type-dependent data */
  1029. ieee80211_setup_sdata(sdata, type);
  1030. if (params) {
  1031. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  1032. if (type == NL80211_IFTYPE_STATION)
  1033. sdata->u.mgd.use_4addr = params->use_4addr;
  1034. }
  1035. ret = register_netdevice(ndev);
  1036. if (ret)
  1037. goto fail;
  1038. mutex_lock(&local->iflist_mtx);
  1039. list_add_tail_rcu(&sdata->list, &local->interfaces);
  1040. mutex_unlock(&local->iflist_mtx);
  1041. if (new_dev)
  1042. *new_dev = ndev;
  1043. return 0;
  1044. fail:
  1045. free_netdev(ndev);
  1046. return ret;
  1047. }
  1048. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  1049. {
  1050. ASSERT_RTNL();
  1051. mutex_lock(&sdata->local->iflist_mtx);
  1052. list_del_rcu(&sdata->list);
  1053. mutex_unlock(&sdata->local->iflist_mtx);
  1054. if (ieee80211_vif_is_mesh(&sdata->vif))
  1055. mesh_path_flush_by_iface(sdata);
  1056. synchronize_rcu();
  1057. unregister_netdevice(sdata->dev);
  1058. }
  1059. /*
  1060. * Remove all interfaces, may only be called at hardware unregistration
  1061. * time because it doesn't do RCU-safe list removals.
  1062. */
  1063. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  1064. {
  1065. struct ieee80211_sub_if_data *sdata, *tmp;
  1066. LIST_HEAD(unreg_list);
  1067. ASSERT_RTNL();
  1068. /*
  1069. * Close all AP_VLAN interfaces first, as otherwise they
  1070. * might be closed while the AP interface they belong to
  1071. * is closed, causing unregister_netdevice_many() to crash.
  1072. */
  1073. list_for_each_entry(sdata, &local->interfaces, list)
  1074. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1075. dev_close(sdata->dev);
  1076. mutex_lock(&local->iflist_mtx);
  1077. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  1078. list_del(&sdata->list);
  1079. if (ieee80211_vif_is_mesh(&sdata->vif))
  1080. mesh_path_flush_by_iface(sdata);
  1081. unregister_netdevice_queue(sdata->dev, &unreg_list);
  1082. }
  1083. mutex_unlock(&local->iflist_mtx);
  1084. unregister_netdevice_many(&unreg_list);
  1085. list_del(&unreg_list);
  1086. }
  1087. static u32 ieee80211_idle_off(struct ieee80211_local *local,
  1088. const char *reason)
  1089. {
  1090. if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
  1091. return 0;
  1092. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1093. wiphy_debug(local->hw.wiphy, "device no longer idle - %s\n", reason);
  1094. #endif
  1095. local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
  1096. return IEEE80211_CONF_CHANGE_IDLE;
  1097. }
  1098. static u32 ieee80211_idle_on(struct ieee80211_local *local)
  1099. {
  1100. if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
  1101. return 0;
  1102. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1103. wiphy_debug(local->hw.wiphy, "device now idle\n");
  1104. #endif
  1105. drv_flush(local, false);
  1106. local->hw.conf.flags |= IEEE80211_CONF_IDLE;
  1107. return IEEE80211_CONF_CHANGE_IDLE;
  1108. }
  1109. u32 __ieee80211_recalc_idle(struct ieee80211_local *local)
  1110. {
  1111. struct ieee80211_sub_if_data *sdata;
  1112. int count = 0;
  1113. bool working = false, scanning = false, hw_roc = false;
  1114. struct ieee80211_work *wk;
  1115. unsigned int led_trig_start = 0, led_trig_stop = 0;
  1116. #ifdef CONFIG_PROVE_LOCKING
  1117. WARN_ON(debug_locks && !lockdep_rtnl_is_held() &&
  1118. !lockdep_is_held(&local->iflist_mtx));
  1119. #endif
  1120. lockdep_assert_held(&local->mtx);
  1121. list_for_each_entry(sdata, &local->interfaces, list) {
  1122. if (!ieee80211_sdata_running(sdata)) {
  1123. sdata->vif.bss_conf.idle = true;
  1124. continue;
  1125. }
  1126. sdata->old_idle = sdata->vif.bss_conf.idle;
  1127. /* do not count disabled managed interfaces */
  1128. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  1129. !sdata->u.mgd.associated &&
  1130. !sdata->u.mgd.auth_data &&
  1131. !sdata->u.mgd.assoc_data) {
  1132. sdata->vif.bss_conf.idle = true;
  1133. continue;
  1134. }
  1135. /* do not count unused IBSS interfaces */
  1136. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  1137. !sdata->u.ibss.ssid_len) {
  1138. sdata->vif.bss_conf.idle = true;
  1139. continue;
  1140. }
  1141. /* count everything else */
  1142. sdata->vif.bss_conf.idle = false;
  1143. count++;
  1144. }
  1145. list_for_each_entry(wk, &local->work_list, list) {
  1146. working = true;
  1147. wk->sdata->vif.bss_conf.idle = false;
  1148. }
  1149. if (local->scan_sdata &&
  1150. !(local->hw.flags & IEEE80211_HW_SCAN_WHILE_IDLE)) {
  1151. scanning = true;
  1152. local->scan_sdata->vif.bss_conf.idle = false;
  1153. }
  1154. if (local->hw_roc_channel)
  1155. hw_roc = true;
  1156. list_for_each_entry(sdata, &local->interfaces, list) {
  1157. if (sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1158. sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  1159. continue;
  1160. if (sdata->old_idle == sdata->vif.bss_conf.idle)
  1161. continue;
  1162. if (!ieee80211_sdata_running(sdata))
  1163. continue;
  1164. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
  1165. }
  1166. if (working || scanning || hw_roc)
  1167. led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_WORK;
  1168. else
  1169. led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_WORK;
  1170. if (count)
  1171. led_trig_start |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
  1172. else
  1173. led_trig_stop |= IEEE80211_TPT_LEDTRIG_FL_CONNECTED;
  1174. ieee80211_mod_tpt_led_trig(local, led_trig_start, led_trig_stop);
  1175. if (hw_roc)
  1176. return ieee80211_idle_off(local, "hw remain-on-channel");
  1177. if (working)
  1178. return ieee80211_idle_off(local, "working");
  1179. if (scanning)
  1180. return ieee80211_idle_off(local, "scanning");
  1181. if (!count)
  1182. return ieee80211_idle_on(local);
  1183. else
  1184. return ieee80211_idle_off(local, "in use");
  1185. return 0;
  1186. }
  1187. void ieee80211_recalc_idle(struct ieee80211_local *local)
  1188. {
  1189. u32 chg;
  1190. mutex_lock(&local->iflist_mtx);
  1191. chg = __ieee80211_recalc_idle(local);
  1192. mutex_unlock(&local->iflist_mtx);
  1193. if (chg)
  1194. ieee80211_hw_config(local, chg);
  1195. }
  1196. static int netdev_notify(struct notifier_block *nb,
  1197. unsigned long state,
  1198. void *ndev)
  1199. {
  1200. struct net_device *dev = ndev;
  1201. struct ieee80211_sub_if_data *sdata;
  1202. if (state != NETDEV_CHANGENAME)
  1203. return 0;
  1204. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  1205. return 0;
  1206. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  1207. return 0;
  1208. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1209. memcpy(sdata->name, dev->name, IFNAMSIZ);
  1210. ieee80211_debugfs_rename_netdev(sdata);
  1211. return 0;
  1212. }
  1213. static struct notifier_block mac80211_netdev_notifier = {
  1214. .notifier_call = netdev_notify,
  1215. };
  1216. int ieee80211_iface_init(void)
  1217. {
  1218. return register_netdevice_notifier(&mac80211_netdev_notifier);
  1219. }
  1220. void ieee80211_iface_exit(void)
  1221. {
  1222. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  1223. }