iface.c 39 KB

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