iface.c 42 KB

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