iface.c 40 KB

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