iface.c 42 KB

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