iface.c 40 KB

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