iface.c 40 KB

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