iface.c 43 KB

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