iface.c 42 KB

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