iface.c 42 KB

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