iface.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  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. cancel_work_sync(&sdata->recalc_smps);
  622. /* APs need special treatment */
  623. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  624. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  625. /* down all dependent devices, that is VLANs */
  626. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  627. u.vlan.list)
  628. dev_close(vlan->dev);
  629. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  630. } else if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  631. ieee80211_mgd_stop(sdata);
  632. }
  633. if (going_down)
  634. local->open_count--;
  635. switch (sdata->vif.type) {
  636. case NL80211_IFTYPE_AP_VLAN:
  637. list_del(&sdata->u.vlan.list);
  638. /* no need to tell driver */
  639. break;
  640. case NL80211_IFTYPE_MONITOR:
  641. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  642. local->cooked_mntrs--;
  643. break;
  644. }
  645. local->monitors--;
  646. if (local->monitors == 0) {
  647. local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
  648. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  649. ieee80211_del_virtual_monitor(local);
  650. }
  651. ieee80211_adjust_monitor_flags(sdata, -1);
  652. ieee80211_configure_filter(local);
  653. break;
  654. case NL80211_IFTYPE_P2P_DEVICE:
  655. /* relies on synchronize_rcu() below */
  656. rcu_assign_pointer(local->p2p_sdata, NULL);
  657. /* fall through */
  658. default:
  659. flush_work(&sdata->work);
  660. /*
  661. * When we get here, the interface is marked down.
  662. * Call rcu_barrier() to wait both for the RX path
  663. * should it be using the interface and enqueuing
  664. * frames at this very time on another CPU, and
  665. * for the sta free call_rcu callbacks.
  666. */
  667. rcu_barrier();
  668. /*
  669. * free_sta_rcu() enqueues a work for the actual
  670. * sta cleanup, so we need to flush it while
  671. * sdata is still valid.
  672. */
  673. flush_workqueue(local->workqueue);
  674. skb_queue_purge(&sdata->skb_queue);
  675. /*
  676. * Free all remaining keys, there shouldn't be any,
  677. * except maybe group keys in AP more or WDS?
  678. */
  679. ieee80211_free_keys(sdata);
  680. if (going_down)
  681. drv_remove_interface(local, sdata);
  682. }
  683. sdata->bss = NULL;
  684. mutex_lock(&local->mtx);
  685. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  686. mutex_unlock(&local->mtx);
  687. ieee80211_recalc_ps(local, -1);
  688. if (local->open_count == 0) {
  689. if (local->ops->napi_poll)
  690. napi_disable(&local->napi);
  691. ieee80211_clear_tx_pending(local);
  692. ieee80211_stop_device(local);
  693. /* no reconfiguring after stop! */
  694. hw_reconf_flags = 0;
  695. }
  696. /* do after stop to avoid reconfiguring when we stop anyway */
  697. if (hw_reconf_flags)
  698. ieee80211_hw_config(local, hw_reconf_flags);
  699. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  700. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  701. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  702. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  703. if (info->control.vif == &sdata->vif) {
  704. __skb_unlink(skb, &local->pending[i]);
  705. dev_kfree_skb_irq(skb);
  706. }
  707. }
  708. }
  709. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  710. if (local->monitors == local->open_count && local->monitors > 0)
  711. ieee80211_add_virtual_monitor(local);
  712. }
  713. static int ieee80211_stop(struct net_device *dev)
  714. {
  715. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  716. ieee80211_do_stop(sdata, true);
  717. return 0;
  718. }
  719. static void ieee80211_set_multicast_list(struct net_device *dev)
  720. {
  721. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  722. struct ieee80211_local *local = sdata->local;
  723. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  724. allmulti = !!(dev->flags & IFF_ALLMULTI);
  725. promisc = !!(dev->flags & IFF_PROMISC);
  726. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  727. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  728. if (allmulti != sdata_allmulti) {
  729. if (dev->flags & IFF_ALLMULTI)
  730. atomic_inc(&local->iff_allmultis);
  731. else
  732. atomic_dec(&local->iff_allmultis);
  733. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  734. }
  735. if (promisc != sdata_promisc) {
  736. if (dev->flags & IFF_PROMISC)
  737. atomic_inc(&local->iff_promiscs);
  738. else
  739. atomic_dec(&local->iff_promiscs);
  740. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  741. }
  742. spin_lock_bh(&local->filter_lock);
  743. __hw_addr_sync(&local->mc_list, &dev->mc, dev->addr_len);
  744. spin_unlock_bh(&local->filter_lock);
  745. ieee80211_queue_work(&local->hw, &local->reconfig_filter);
  746. }
  747. /*
  748. * Called when the netdev is removed or, by the code below, before
  749. * the interface type changes.
  750. */
  751. static void ieee80211_teardown_sdata(struct ieee80211_sub_if_data *sdata)
  752. {
  753. struct ieee80211_local *local = sdata->local;
  754. int flushed;
  755. int i;
  756. /* free extra data */
  757. ieee80211_free_keys(sdata);
  758. ieee80211_debugfs_remove_netdev(sdata);
  759. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  760. __skb_queue_purge(&sdata->fragments[i].skb_list);
  761. sdata->fragment_next = 0;
  762. if (ieee80211_vif_is_mesh(&sdata->vif))
  763. mesh_rmc_free(sdata);
  764. flushed = sta_info_flush(local, sdata);
  765. WARN_ON(flushed);
  766. }
  767. static void ieee80211_uninit(struct net_device *dev)
  768. {
  769. ieee80211_teardown_sdata(IEEE80211_DEV_TO_SUB_IF(dev));
  770. }
  771. static u16 ieee80211_netdev_select_queue(struct net_device *dev,
  772. struct sk_buff *skb)
  773. {
  774. return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
  775. }
  776. static const struct net_device_ops ieee80211_dataif_ops = {
  777. .ndo_open = ieee80211_open,
  778. .ndo_stop = ieee80211_stop,
  779. .ndo_uninit = ieee80211_uninit,
  780. .ndo_start_xmit = ieee80211_subif_start_xmit,
  781. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  782. .ndo_change_mtu = ieee80211_change_mtu,
  783. .ndo_set_mac_address = ieee80211_change_mac,
  784. .ndo_select_queue = ieee80211_netdev_select_queue,
  785. };
  786. static u16 ieee80211_monitor_select_queue(struct net_device *dev,
  787. struct sk_buff *skb)
  788. {
  789. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  790. struct ieee80211_local *local = sdata->local;
  791. struct ieee80211_hdr *hdr;
  792. struct ieee80211_radiotap_header *rtap = (void *)skb->data;
  793. if (local->hw.queues < IEEE80211_NUM_ACS)
  794. return 0;
  795. if (skb->len < 4 ||
  796. skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
  797. return 0; /* doesn't matter, frame will be dropped */
  798. hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
  799. return ieee80211_select_queue_80211(sdata, skb, hdr);
  800. }
  801. static const struct net_device_ops ieee80211_monitorif_ops = {
  802. .ndo_open = ieee80211_open,
  803. .ndo_stop = ieee80211_stop,
  804. .ndo_uninit = ieee80211_uninit,
  805. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  806. .ndo_set_rx_mode = ieee80211_set_multicast_list,
  807. .ndo_change_mtu = ieee80211_change_mtu,
  808. .ndo_set_mac_address = eth_mac_addr,
  809. .ndo_select_queue = ieee80211_monitor_select_queue,
  810. };
  811. static void ieee80211_if_setup(struct net_device *dev)
  812. {
  813. ether_setup(dev);
  814. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  815. dev->netdev_ops = &ieee80211_dataif_ops;
  816. dev->destructor = free_netdev;
  817. }
  818. static void ieee80211_iface_work(struct work_struct *work)
  819. {
  820. struct ieee80211_sub_if_data *sdata =
  821. container_of(work, struct ieee80211_sub_if_data, work);
  822. struct ieee80211_local *local = sdata->local;
  823. struct sk_buff *skb;
  824. struct sta_info *sta;
  825. struct ieee80211_ra_tid *ra_tid;
  826. if (!ieee80211_sdata_running(sdata))
  827. return;
  828. if (local->scanning)
  829. return;
  830. /*
  831. * ieee80211_queue_work() should have picked up most cases,
  832. * here we'll pick the rest.
  833. */
  834. if (WARN(local->suspended,
  835. "interface work scheduled while going to suspend\n"))
  836. return;
  837. /* first process frames */
  838. while ((skb = skb_dequeue(&sdata->skb_queue))) {
  839. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  840. if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_START) {
  841. ra_tid = (void *)&skb->cb;
  842. ieee80211_start_tx_ba_cb(&sdata->vif, ra_tid->ra,
  843. ra_tid->tid);
  844. } else if (skb->pkt_type == IEEE80211_SDATA_QUEUE_AGG_STOP) {
  845. ra_tid = (void *)&skb->cb;
  846. ieee80211_stop_tx_ba_cb(&sdata->vif, ra_tid->ra,
  847. ra_tid->tid);
  848. } else if (ieee80211_is_action(mgmt->frame_control) &&
  849. mgmt->u.action.category == WLAN_CATEGORY_BACK) {
  850. int len = skb->len;
  851. mutex_lock(&local->sta_mtx);
  852. sta = sta_info_get_bss(sdata, mgmt->sa);
  853. if (sta) {
  854. switch (mgmt->u.action.u.addba_req.action_code) {
  855. case WLAN_ACTION_ADDBA_REQ:
  856. ieee80211_process_addba_request(
  857. local, sta, mgmt, len);
  858. break;
  859. case WLAN_ACTION_ADDBA_RESP:
  860. ieee80211_process_addba_resp(local, sta,
  861. mgmt, len);
  862. break;
  863. case WLAN_ACTION_DELBA:
  864. ieee80211_process_delba(sdata, sta,
  865. mgmt, len);
  866. break;
  867. default:
  868. WARN_ON(1);
  869. break;
  870. }
  871. }
  872. mutex_unlock(&local->sta_mtx);
  873. } else if (ieee80211_is_data_qos(mgmt->frame_control)) {
  874. struct ieee80211_hdr *hdr = (void *)mgmt;
  875. /*
  876. * So the frame isn't mgmt, but frame_control
  877. * is at the right place anyway, of course, so
  878. * the if statement is correct.
  879. *
  880. * Warn if we have other data frame types here,
  881. * they must not get here.
  882. */
  883. WARN_ON(hdr->frame_control &
  884. cpu_to_le16(IEEE80211_STYPE_NULLFUNC));
  885. WARN_ON(!(hdr->seq_ctrl &
  886. cpu_to_le16(IEEE80211_SCTL_FRAG)));
  887. /*
  888. * This was a fragment of a frame, received while
  889. * a block-ack session was active. That cannot be
  890. * right, so terminate the session.
  891. */
  892. mutex_lock(&local->sta_mtx);
  893. sta = sta_info_get_bss(sdata, mgmt->sa);
  894. if (sta) {
  895. u16 tid = *ieee80211_get_qos_ctl(hdr) &
  896. IEEE80211_QOS_CTL_TID_MASK;
  897. __ieee80211_stop_rx_ba_session(
  898. sta, tid, WLAN_BACK_RECIPIENT,
  899. WLAN_REASON_QSTA_REQUIRE_SETUP,
  900. true);
  901. }
  902. mutex_unlock(&local->sta_mtx);
  903. } else switch (sdata->vif.type) {
  904. case NL80211_IFTYPE_STATION:
  905. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  906. break;
  907. case NL80211_IFTYPE_ADHOC:
  908. ieee80211_ibss_rx_queued_mgmt(sdata, skb);
  909. break;
  910. case NL80211_IFTYPE_MESH_POINT:
  911. if (!ieee80211_vif_is_mesh(&sdata->vif))
  912. break;
  913. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  914. break;
  915. default:
  916. WARN(1, "frame for unexpected interface type");
  917. break;
  918. }
  919. kfree_skb(skb);
  920. }
  921. /* then other type-dependent work */
  922. switch (sdata->vif.type) {
  923. case NL80211_IFTYPE_STATION:
  924. ieee80211_sta_work(sdata);
  925. break;
  926. case NL80211_IFTYPE_ADHOC:
  927. ieee80211_ibss_work(sdata);
  928. break;
  929. case NL80211_IFTYPE_MESH_POINT:
  930. if (!ieee80211_vif_is_mesh(&sdata->vif))
  931. break;
  932. ieee80211_mesh_work(sdata);
  933. break;
  934. default:
  935. break;
  936. }
  937. }
  938. static void ieee80211_recalc_smps_work(struct work_struct *work)
  939. {
  940. struct ieee80211_sub_if_data *sdata =
  941. container_of(work, struct ieee80211_sub_if_data, recalc_smps);
  942. ieee80211_recalc_smps(sdata);
  943. }
  944. /*
  945. * Helper function to initialise an interface to a specific type.
  946. */
  947. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  948. enum nl80211_iftype type)
  949. {
  950. /* clear type-dependent union */
  951. memset(&sdata->u, 0, sizeof(sdata->u));
  952. /* and set some type-dependent values */
  953. sdata->vif.type = type;
  954. sdata->vif.p2p = false;
  955. sdata->wdev.iftype = type;
  956. sdata->control_port_protocol = cpu_to_be16(ETH_P_PAE);
  957. sdata->control_port_no_encrypt = false;
  958. sdata->noack_map = 0;
  959. /* only monitor/p2p-device differ */
  960. if (sdata->dev) {
  961. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  962. sdata->dev->type = ARPHRD_ETHER;
  963. }
  964. skb_queue_head_init(&sdata->skb_queue);
  965. INIT_WORK(&sdata->work, ieee80211_iface_work);
  966. INIT_WORK(&sdata->recalc_smps, ieee80211_recalc_smps_work);
  967. switch (type) {
  968. case NL80211_IFTYPE_P2P_GO:
  969. type = NL80211_IFTYPE_AP;
  970. sdata->vif.type = type;
  971. sdata->vif.p2p = true;
  972. /* fall through */
  973. case NL80211_IFTYPE_AP:
  974. skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
  975. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  976. break;
  977. case NL80211_IFTYPE_P2P_CLIENT:
  978. type = NL80211_IFTYPE_STATION;
  979. sdata->vif.type = type;
  980. sdata->vif.p2p = true;
  981. /* fall through */
  982. case NL80211_IFTYPE_STATION:
  983. ieee80211_sta_setup_sdata(sdata);
  984. break;
  985. case NL80211_IFTYPE_ADHOC:
  986. ieee80211_ibss_setup_sdata(sdata);
  987. break;
  988. case NL80211_IFTYPE_MESH_POINT:
  989. if (ieee80211_vif_is_mesh(&sdata->vif))
  990. ieee80211_mesh_init_sdata(sdata);
  991. break;
  992. case NL80211_IFTYPE_MONITOR:
  993. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  994. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  995. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  996. MONITOR_FLAG_OTHER_BSS;
  997. break;
  998. case NL80211_IFTYPE_WDS:
  999. case NL80211_IFTYPE_AP_VLAN:
  1000. case NL80211_IFTYPE_P2P_DEVICE:
  1001. break;
  1002. case NL80211_IFTYPE_UNSPECIFIED:
  1003. case NUM_NL80211_IFTYPES:
  1004. BUG();
  1005. break;
  1006. }
  1007. ieee80211_debugfs_add_netdev(sdata);
  1008. }
  1009. static int ieee80211_runtime_change_iftype(struct ieee80211_sub_if_data *sdata,
  1010. enum nl80211_iftype type)
  1011. {
  1012. struct ieee80211_local *local = sdata->local;
  1013. int ret, err;
  1014. enum nl80211_iftype internal_type = type;
  1015. bool p2p = false;
  1016. ASSERT_RTNL();
  1017. if (!local->ops->change_interface)
  1018. return -EBUSY;
  1019. switch (sdata->vif.type) {
  1020. case NL80211_IFTYPE_AP:
  1021. case NL80211_IFTYPE_STATION:
  1022. case NL80211_IFTYPE_ADHOC:
  1023. /*
  1024. * Could maybe also all others here?
  1025. * Just not sure how that interacts
  1026. * with the RX/config path e.g. for
  1027. * mesh.
  1028. */
  1029. break;
  1030. default:
  1031. return -EBUSY;
  1032. }
  1033. switch (type) {
  1034. case NL80211_IFTYPE_AP:
  1035. case NL80211_IFTYPE_STATION:
  1036. case NL80211_IFTYPE_ADHOC:
  1037. /*
  1038. * Could probably support everything
  1039. * but WDS here (WDS do_open can fail
  1040. * under memory pressure, which this
  1041. * code isn't prepared to handle).
  1042. */
  1043. break;
  1044. case NL80211_IFTYPE_P2P_CLIENT:
  1045. p2p = true;
  1046. internal_type = NL80211_IFTYPE_STATION;
  1047. break;
  1048. case NL80211_IFTYPE_P2P_GO:
  1049. p2p = true;
  1050. internal_type = NL80211_IFTYPE_AP;
  1051. break;
  1052. default:
  1053. return -EBUSY;
  1054. }
  1055. ret = ieee80211_check_concurrent_iface(sdata, internal_type);
  1056. if (ret)
  1057. return ret;
  1058. ieee80211_do_stop(sdata, false);
  1059. ieee80211_teardown_sdata(sdata);
  1060. ret = drv_change_interface(local, sdata, internal_type, p2p);
  1061. if (ret)
  1062. type = sdata->vif.type;
  1063. /*
  1064. * Ignore return value here, there's not much we can do since
  1065. * the driver changed the interface type internally already.
  1066. * The warnings will hopefully make driver authors fix it :-)
  1067. */
  1068. ieee80211_check_queues(sdata);
  1069. ieee80211_setup_sdata(sdata, type);
  1070. err = ieee80211_do_open(&sdata->wdev, false);
  1071. WARN(err, "type change: do_open returned %d", err);
  1072. return ret;
  1073. }
  1074. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  1075. enum nl80211_iftype type)
  1076. {
  1077. int ret;
  1078. ASSERT_RTNL();
  1079. if (type == ieee80211_vif_type_p2p(&sdata->vif))
  1080. return 0;
  1081. if (ieee80211_sdata_running(sdata)) {
  1082. ret = ieee80211_runtime_change_iftype(sdata, type);
  1083. if (ret)
  1084. return ret;
  1085. } else {
  1086. /* Purge and reset type-dependent state. */
  1087. ieee80211_teardown_sdata(sdata);
  1088. ieee80211_setup_sdata(sdata, type);
  1089. }
  1090. /* reset some values that shouldn't be kept across type changes */
  1091. sdata->drop_unencrypted = 0;
  1092. if (type == NL80211_IFTYPE_STATION)
  1093. sdata->u.mgd.use_4addr = false;
  1094. return 0;
  1095. }
  1096. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  1097. u8 *perm_addr, enum nl80211_iftype type)
  1098. {
  1099. struct ieee80211_sub_if_data *sdata;
  1100. u64 mask, start, addr, val, inc;
  1101. u8 *m;
  1102. u8 tmp_addr[ETH_ALEN];
  1103. int i;
  1104. /* default ... something at least */
  1105. memcpy(perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1106. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  1107. local->hw.wiphy->n_addresses <= 1)
  1108. return;
  1109. mutex_lock(&local->iflist_mtx);
  1110. switch (type) {
  1111. case NL80211_IFTYPE_MONITOR:
  1112. /* doesn't matter */
  1113. break;
  1114. case NL80211_IFTYPE_WDS:
  1115. case NL80211_IFTYPE_AP_VLAN:
  1116. /* match up with an AP interface */
  1117. list_for_each_entry(sdata, &local->interfaces, list) {
  1118. if (sdata->vif.type != NL80211_IFTYPE_AP)
  1119. continue;
  1120. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1121. break;
  1122. }
  1123. /* keep default if no AP interface present */
  1124. break;
  1125. case NL80211_IFTYPE_P2P_CLIENT:
  1126. case NL80211_IFTYPE_P2P_GO:
  1127. if (local->hw.flags & IEEE80211_HW_P2P_DEV_ADDR_FOR_INTF) {
  1128. list_for_each_entry(sdata, &local->interfaces, list) {
  1129. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE)
  1130. continue;
  1131. if (!ieee80211_sdata_running(sdata))
  1132. continue;
  1133. memcpy(perm_addr, sdata->vif.addr, ETH_ALEN);
  1134. goto out_unlock;
  1135. }
  1136. }
  1137. /* otherwise fall through */
  1138. default:
  1139. /* assign a new address if possible -- try n_addresses first */
  1140. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  1141. bool used = false;
  1142. list_for_each_entry(sdata, &local->interfaces, list) {
  1143. if (memcmp(local->hw.wiphy->addresses[i].addr,
  1144. sdata->vif.addr, ETH_ALEN) == 0) {
  1145. used = true;
  1146. break;
  1147. }
  1148. }
  1149. if (!used) {
  1150. memcpy(perm_addr,
  1151. local->hw.wiphy->addresses[i].addr,
  1152. ETH_ALEN);
  1153. break;
  1154. }
  1155. }
  1156. /* try mask if available */
  1157. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  1158. break;
  1159. m = local->hw.wiphy->addr_mask;
  1160. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1161. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1162. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1163. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  1164. /* not a contiguous mask ... not handled now! */
  1165. pr_info("not contiguous\n");
  1166. break;
  1167. }
  1168. m = local->hw.wiphy->perm_addr;
  1169. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  1170. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  1171. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  1172. inc = 1ULL<<__ffs64(mask);
  1173. val = (start & mask);
  1174. addr = (start & ~mask) | (val & mask);
  1175. do {
  1176. bool used = false;
  1177. tmp_addr[5] = addr >> 0*8;
  1178. tmp_addr[4] = addr >> 1*8;
  1179. tmp_addr[3] = addr >> 2*8;
  1180. tmp_addr[2] = addr >> 3*8;
  1181. tmp_addr[1] = addr >> 4*8;
  1182. tmp_addr[0] = addr >> 5*8;
  1183. val += inc;
  1184. list_for_each_entry(sdata, &local->interfaces, list) {
  1185. if (memcmp(tmp_addr, sdata->vif.addr,
  1186. ETH_ALEN) == 0) {
  1187. used = true;
  1188. break;
  1189. }
  1190. }
  1191. if (!used) {
  1192. memcpy(perm_addr, tmp_addr, ETH_ALEN);
  1193. break;
  1194. }
  1195. addr = (start & ~mask) | (val & mask);
  1196. } while (addr != start);
  1197. break;
  1198. }
  1199. out_unlock:
  1200. mutex_unlock(&local->iflist_mtx);
  1201. }
  1202. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  1203. struct wireless_dev **new_wdev, enum nl80211_iftype type,
  1204. struct vif_params *params)
  1205. {
  1206. struct net_device *ndev = NULL;
  1207. struct ieee80211_sub_if_data *sdata = NULL;
  1208. int ret, i;
  1209. int txqs = 1;
  1210. ASSERT_RTNL();
  1211. if (type == NL80211_IFTYPE_P2P_DEVICE) {
  1212. struct wireless_dev *wdev;
  1213. sdata = kzalloc(sizeof(*sdata) + local->hw.vif_data_size,
  1214. GFP_KERNEL);
  1215. if (!sdata)
  1216. return -ENOMEM;
  1217. wdev = &sdata->wdev;
  1218. sdata->dev = NULL;
  1219. strlcpy(sdata->name, name, IFNAMSIZ);
  1220. ieee80211_assign_perm_addr(local, wdev->address, type);
  1221. memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
  1222. } else {
  1223. if (local->hw.queues >= IEEE80211_NUM_ACS)
  1224. txqs = IEEE80211_NUM_ACS;
  1225. ndev = alloc_netdev_mqs(sizeof(*sdata) +
  1226. local->hw.vif_data_size,
  1227. name, ieee80211_if_setup, txqs, 1);
  1228. if (!ndev)
  1229. return -ENOMEM;
  1230. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  1231. ndev->needed_headroom = local->tx_headroom +
  1232. 4*6 /* four MAC addresses */
  1233. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  1234. + 6 /* mesh */
  1235. + 8 /* rfc1042/bridge tunnel */
  1236. - ETH_HLEN /* ethernet hard_header_len */
  1237. + IEEE80211_ENCRYPT_HEADROOM;
  1238. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  1239. ret = dev_alloc_name(ndev, ndev->name);
  1240. if (ret < 0) {
  1241. free_netdev(ndev);
  1242. return ret;
  1243. }
  1244. ieee80211_assign_perm_addr(local, ndev->perm_addr, type);
  1245. memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
  1246. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  1247. /* don't use IEEE80211_DEV_TO_SUB_IF -- it checks too much */
  1248. sdata = netdev_priv(ndev);
  1249. ndev->ieee80211_ptr = &sdata->wdev;
  1250. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  1251. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  1252. sdata->dev = ndev;
  1253. }
  1254. /* initialise type-independent data */
  1255. sdata->wdev.wiphy = local->hw.wiphy;
  1256. sdata->local = local;
  1257. #ifdef CONFIG_INET
  1258. sdata->arp_filter_state = true;
  1259. #endif
  1260. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  1261. skb_queue_head_init(&sdata->fragments[i].skb_list);
  1262. INIT_LIST_HEAD(&sdata->key_list);
  1263. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  1264. struct ieee80211_supported_band *sband;
  1265. sband = local->hw.wiphy->bands[i];
  1266. sdata->rc_rateidx_mask[i] =
  1267. sband ? (1 << sband->n_bitrates) - 1 : 0;
  1268. if (sband)
  1269. memcpy(sdata->rc_rateidx_mcs_mask[i],
  1270. sband->ht_cap.mcs.rx_mask,
  1271. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1272. else
  1273. memset(sdata->rc_rateidx_mcs_mask[i], 0,
  1274. sizeof(sdata->rc_rateidx_mcs_mask[i]));
  1275. }
  1276. ieee80211_set_default_queues(sdata);
  1277. /* setup type-dependent data */
  1278. ieee80211_setup_sdata(sdata, type);
  1279. if (ndev) {
  1280. if (params) {
  1281. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  1282. if (type == NL80211_IFTYPE_STATION)
  1283. sdata->u.mgd.use_4addr = params->use_4addr;
  1284. }
  1285. ndev->features |= local->hw.netdev_features;
  1286. ret = register_netdevice(ndev);
  1287. if (ret) {
  1288. free_netdev(ndev);
  1289. return ret;
  1290. }
  1291. }
  1292. mutex_lock(&local->iflist_mtx);
  1293. list_add_tail_rcu(&sdata->list, &local->interfaces);
  1294. mutex_unlock(&local->iflist_mtx);
  1295. if (new_wdev)
  1296. *new_wdev = &sdata->wdev;
  1297. return 0;
  1298. }
  1299. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  1300. {
  1301. ASSERT_RTNL();
  1302. mutex_lock(&sdata->local->iflist_mtx);
  1303. list_del_rcu(&sdata->list);
  1304. mutex_unlock(&sdata->local->iflist_mtx);
  1305. synchronize_rcu();
  1306. if (sdata->dev) {
  1307. unregister_netdevice(sdata->dev);
  1308. } else {
  1309. cfg80211_unregister_wdev(&sdata->wdev);
  1310. kfree(sdata);
  1311. }
  1312. }
  1313. void ieee80211_sdata_stop(struct ieee80211_sub_if_data *sdata)
  1314. {
  1315. if (WARN_ON_ONCE(!test_bit(SDATA_STATE_RUNNING, &sdata->state)))
  1316. return;
  1317. ieee80211_do_stop(sdata, true);
  1318. ieee80211_teardown_sdata(sdata);
  1319. }
  1320. /*
  1321. * Remove all interfaces, may only be called at hardware unregistration
  1322. * time because it doesn't do RCU-safe list removals.
  1323. */
  1324. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  1325. {
  1326. struct ieee80211_sub_if_data *sdata, *tmp;
  1327. LIST_HEAD(unreg_list);
  1328. LIST_HEAD(wdev_list);
  1329. ASSERT_RTNL();
  1330. mutex_lock(&local->iflist_mtx);
  1331. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  1332. list_del(&sdata->list);
  1333. if (sdata->dev)
  1334. unregister_netdevice_queue(sdata->dev, &unreg_list);
  1335. else
  1336. list_add(&sdata->list, &wdev_list);
  1337. }
  1338. mutex_unlock(&local->iflist_mtx);
  1339. unregister_netdevice_many(&unreg_list);
  1340. list_del(&unreg_list);
  1341. list_for_each_entry_safe(sdata, tmp, &wdev_list, list) {
  1342. list_del(&sdata->list);
  1343. cfg80211_unregister_wdev(&sdata->wdev);
  1344. kfree(sdata);
  1345. }
  1346. }
  1347. static int netdev_notify(struct notifier_block *nb,
  1348. unsigned long state,
  1349. void *ndev)
  1350. {
  1351. struct net_device *dev = ndev;
  1352. struct ieee80211_sub_if_data *sdata;
  1353. if (state != NETDEV_CHANGENAME)
  1354. return 0;
  1355. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  1356. return 0;
  1357. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  1358. return 0;
  1359. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  1360. memcpy(sdata->name, dev->name, IFNAMSIZ);
  1361. ieee80211_debugfs_rename_netdev(sdata);
  1362. return 0;
  1363. }
  1364. static struct notifier_block mac80211_netdev_notifier = {
  1365. .notifier_call = netdev_notify,
  1366. };
  1367. int ieee80211_iface_init(void)
  1368. {
  1369. return register_netdevice_notifier(&mac80211_netdev_notifier);
  1370. }
  1371. void ieee80211_iface_exit(void)
  1372. {
  1373. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  1374. }