iface.c 40 KB

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