iface.c 43 KB

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