iface.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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/kernel.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/rtnetlink.h>
  17. #include <net/mac80211.h>
  18. #include <net/ieee80211_radiotap.h>
  19. #include "ieee80211_i.h"
  20. #include "sta_info.h"
  21. #include "debugfs_netdev.h"
  22. #include "mesh.h"
  23. #include "led.h"
  24. #include "driver-ops.h"
  25. #include "wme.h"
  26. /**
  27. * DOC: Interface list locking
  28. *
  29. * The interface list in each struct ieee80211_local is protected
  30. * three-fold:
  31. *
  32. * (1) modifications may only be done under the RTNL
  33. * (2) modifications and readers are protected against each other by
  34. * the iflist_mtx.
  35. * (3) modifications are done in an RCU manner so atomic readers
  36. * can traverse the list in RCU-safe blocks.
  37. *
  38. * As a consequence, reads (traversals) of the list can be protected
  39. * by either the RTNL, the iflist_mtx or RCU.
  40. */
  41. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  42. {
  43. int meshhdrlen;
  44. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  45. meshhdrlen = (sdata->vif.type == NL80211_IFTYPE_MESH_POINT) ? 5 : 0;
  46. /* FIX: what would be proper limits for MTU?
  47. * This interface uses 802.3 frames. */
  48. if (new_mtu < 256 ||
  49. new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
  50. return -EINVAL;
  51. }
  52. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  53. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  54. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  55. dev->mtu = new_mtu;
  56. return 0;
  57. }
  58. static int ieee80211_change_mac(struct net_device *dev, void *addr)
  59. {
  60. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  61. struct sockaddr *sa = addr;
  62. int ret;
  63. if (ieee80211_sdata_running(sdata))
  64. return -EBUSY;
  65. ret = eth_mac_addr(dev, sa);
  66. if (ret == 0)
  67. memcpy(sdata->vif.addr, sa->sa_data, ETH_ALEN);
  68. return ret;
  69. }
  70. static inline int identical_mac_addr_allowed(int type1, int type2)
  71. {
  72. return type1 == NL80211_IFTYPE_MONITOR ||
  73. type2 == NL80211_IFTYPE_MONITOR ||
  74. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_WDS) ||
  75. (type1 == NL80211_IFTYPE_WDS &&
  76. (type2 == NL80211_IFTYPE_WDS ||
  77. type2 == NL80211_IFTYPE_AP)) ||
  78. (type1 == NL80211_IFTYPE_AP && type2 == NL80211_IFTYPE_AP_VLAN) ||
  79. (type1 == NL80211_IFTYPE_AP_VLAN &&
  80. (type2 == NL80211_IFTYPE_AP ||
  81. type2 == NL80211_IFTYPE_AP_VLAN));
  82. }
  83. static int ieee80211_open(struct net_device *dev)
  84. {
  85. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  86. struct ieee80211_sub_if_data *nsdata;
  87. struct ieee80211_local *local = sdata->local;
  88. struct sta_info *sta;
  89. u32 changed = 0;
  90. int res;
  91. u32 hw_reconf_flags = 0;
  92. u8 null_addr[ETH_ALEN] = {0};
  93. /* fail early if user set an invalid address */
  94. if (compare_ether_addr(dev->dev_addr, null_addr) &&
  95. !is_valid_ether_addr(dev->dev_addr))
  96. return -EADDRNOTAVAIL;
  97. /* we hold the RTNL here so can safely walk the list */
  98. list_for_each_entry(nsdata, &local->interfaces, list) {
  99. struct net_device *ndev = nsdata->dev;
  100. if (ndev != dev && ieee80211_sdata_running(nsdata)) {
  101. /*
  102. * Allow only a single IBSS interface to be up at any
  103. * time. This is restricted because beacon distribution
  104. * cannot work properly if both are in the same IBSS.
  105. *
  106. * To remove this restriction we'd have to disallow them
  107. * from setting the same SSID on different IBSS interfaces
  108. * belonging to the same hardware. Then, however, we're
  109. * faced with having to adopt two different TSF timers...
  110. */
  111. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  112. nsdata->vif.type == NL80211_IFTYPE_ADHOC)
  113. return -EBUSY;
  114. /*
  115. * The remaining checks are only performed for interfaces
  116. * with the same MAC address.
  117. */
  118. if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
  119. continue;
  120. /*
  121. * check whether it may have the same address
  122. */
  123. if (!identical_mac_addr_allowed(sdata->vif.type,
  124. nsdata->vif.type))
  125. return -ENOTUNIQ;
  126. /*
  127. * can only add VLANs to enabled APs
  128. */
  129. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
  130. nsdata->vif.type == NL80211_IFTYPE_AP)
  131. sdata->bss = &nsdata->u.ap;
  132. }
  133. }
  134. switch (sdata->vif.type) {
  135. case NL80211_IFTYPE_WDS:
  136. if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
  137. return -ENOLINK;
  138. break;
  139. case NL80211_IFTYPE_AP_VLAN:
  140. if (!sdata->bss)
  141. return -ENOLINK;
  142. list_add(&sdata->u.vlan.list, &sdata->bss->vlans);
  143. break;
  144. case NL80211_IFTYPE_AP:
  145. sdata->bss = &sdata->u.ap;
  146. break;
  147. case NL80211_IFTYPE_MESH_POINT:
  148. if (!ieee80211_vif_is_mesh(&sdata->vif))
  149. break;
  150. /* mesh ifaces must set allmulti to forward mcast traffic */
  151. atomic_inc(&local->iff_allmultis);
  152. break;
  153. case NL80211_IFTYPE_STATION:
  154. case NL80211_IFTYPE_MONITOR:
  155. case NL80211_IFTYPE_ADHOC:
  156. /* no special treatment */
  157. break;
  158. case NL80211_IFTYPE_UNSPECIFIED:
  159. case __NL80211_IFTYPE_AFTER_LAST:
  160. /* cannot happen */
  161. WARN_ON(1);
  162. break;
  163. }
  164. if (local->open_count == 0) {
  165. res = drv_start(local);
  166. if (res)
  167. goto err_del_bss;
  168. /* we're brought up, everything changes */
  169. hw_reconf_flags = ~0;
  170. ieee80211_led_radio(local, true);
  171. }
  172. /*
  173. * Check all interfaces and copy the hopefully now-present
  174. * MAC address to those that have the special null one.
  175. */
  176. list_for_each_entry(nsdata, &local->interfaces, list) {
  177. struct net_device *ndev = nsdata->dev;
  178. /*
  179. * No need to check running since we do not allow
  180. * it to start up with this invalid address.
  181. */
  182. if (compare_ether_addr(null_addr, ndev->dev_addr) == 0) {
  183. memcpy(ndev->dev_addr,
  184. local->hw.wiphy->perm_addr,
  185. ETH_ALEN);
  186. memcpy(ndev->perm_addr, ndev->dev_addr, ETH_ALEN);
  187. }
  188. }
  189. /*
  190. * Validate the MAC address for this device.
  191. */
  192. if (!is_valid_ether_addr(dev->dev_addr)) {
  193. if (!local->open_count)
  194. drv_stop(local);
  195. return -EADDRNOTAVAIL;
  196. }
  197. switch (sdata->vif.type) {
  198. case NL80211_IFTYPE_AP_VLAN:
  199. /* no need to tell driver */
  200. break;
  201. case NL80211_IFTYPE_MONITOR:
  202. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  203. local->cooked_mntrs++;
  204. break;
  205. }
  206. /* must be before the call to ieee80211_configure_filter */
  207. local->monitors++;
  208. if (local->monitors == 1) {
  209. local->hw.conf.flags |= IEEE80211_CONF_MONITOR;
  210. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  211. }
  212. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  213. local->fif_fcsfail++;
  214. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  215. local->fif_plcpfail++;
  216. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL) {
  217. local->fif_control++;
  218. local->fif_pspoll++;
  219. }
  220. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  221. local->fif_other_bss++;
  222. ieee80211_configure_filter(local);
  223. break;
  224. default:
  225. res = drv_add_interface(local, &sdata->vif);
  226. if (res)
  227. goto err_stop;
  228. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  229. local->fif_other_bss++;
  230. ieee80211_configure_filter(local);
  231. ieee80211_start_mesh(sdata);
  232. } else if (sdata->vif.type == NL80211_IFTYPE_AP) {
  233. local->fif_pspoll++;
  234. ieee80211_configure_filter(local);
  235. }
  236. changed |= ieee80211_reset_erp_info(sdata);
  237. ieee80211_bss_info_change_notify(sdata, changed);
  238. ieee80211_enable_keys(sdata);
  239. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  240. netif_carrier_off(dev);
  241. else
  242. netif_carrier_on(dev);
  243. }
  244. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  245. /* Create STA entry for the WDS peer */
  246. sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
  247. GFP_KERNEL);
  248. if (!sta) {
  249. res = -ENOMEM;
  250. goto err_del_interface;
  251. }
  252. /* no locking required since STA is not live yet */
  253. sta->flags |= WLAN_STA_AUTHORIZED;
  254. res = sta_info_insert(sta);
  255. if (res) {
  256. /* STA has been freed */
  257. goto err_del_interface;
  258. }
  259. }
  260. /*
  261. * set_multicast_list will be invoked by the networking core
  262. * which will check whether any increments here were done in
  263. * error and sync them down to the hardware as filter flags.
  264. */
  265. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  266. atomic_inc(&local->iff_allmultis);
  267. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  268. atomic_inc(&local->iff_promiscs);
  269. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  270. local->open_count++;
  271. if (hw_reconf_flags) {
  272. ieee80211_hw_config(local, hw_reconf_flags);
  273. /*
  274. * set default queue parameters so drivers don't
  275. * need to initialise the hardware if the hardware
  276. * doesn't start up with sane defaults
  277. */
  278. ieee80211_set_wmm_default(sdata);
  279. }
  280. ieee80211_recalc_ps(local, -1);
  281. /*
  282. * ieee80211_sta_work is disabled while network interface
  283. * is down. Therefore, some configuration changes may not
  284. * yet be effective. Trigger execution of ieee80211_sta_work
  285. * to fix this.
  286. */
  287. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  288. ieee80211_queue_work(&local->hw, &sdata->u.mgd.work);
  289. netif_tx_start_all_queues(dev);
  290. return 0;
  291. err_del_interface:
  292. drv_remove_interface(local, &sdata->vif);
  293. err_stop:
  294. if (!local->open_count)
  295. drv_stop(local);
  296. err_del_bss:
  297. sdata->bss = NULL;
  298. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  299. list_del(&sdata->u.vlan.list);
  300. return res;
  301. }
  302. static int ieee80211_stop(struct net_device *dev)
  303. {
  304. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  305. struct ieee80211_local *local = sdata->local;
  306. struct sta_info *sta;
  307. unsigned long flags;
  308. struct sk_buff *skb, *tmp;
  309. u32 hw_reconf_flags = 0;
  310. int i;
  311. /*
  312. * Stop TX on this interface first.
  313. */
  314. netif_tx_stop_all_queues(dev);
  315. /*
  316. * Purge work for this interface.
  317. */
  318. ieee80211_work_purge(sdata);
  319. /*
  320. * Now delete all active aggregation sessions.
  321. */
  322. rcu_read_lock();
  323. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  324. if (sta->sdata == sdata)
  325. ieee80211_sta_tear_down_BA_sessions(sta);
  326. }
  327. rcu_read_unlock();
  328. /*
  329. * Remove all stations associated with this interface.
  330. *
  331. * This must be done before calling ops->remove_interface()
  332. * because otherwise we can later invoke ops->sta_notify()
  333. * whenever the STAs are removed, and that invalidates driver
  334. * assumptions about always getting a vif pointer that is valid
  335. * (because if we remove a STA after ops->remove_interface()
  336. * the driver will have removed the vif info already!)
  337. *
  338. * We could relax this and only unlink the stations from the
  339. * hash table and list but keep them on a per-sdata list that
  340. * will be inserted back again when the interface is brought
  341. * up again, but I don't currently see a use case for that,
  342. * except with WDS which gets a STA entry created when it is
  343. * brought up.
  344. */
  345. sta_info_flush(local, sdata);
  346. /*
  347. * Don't count this interface for promisc/allmulti while it
  348. * is down. dev_mc_unsync() will invoke set_multicast_list
  349. * on the master interface which will sync these down to the
  350. * hardware as filter flags.
  351. */
  352. if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
  353. atomic_dec(&local->iff_allmultis);
  354. if (sdata->flags & IEEE80211_SDATA_PROMISC)
  355. atomic_dec(&local->iff_promiscs);
  356. if (sdata->vif.type == NL80211_IFTYPE_AP)
  357. local->fif_pspoll--;
  358. netif_addr_lock_bh(dev);
  359. spin_lock_bh(&local->filter_lock);
  360. __dev_addr_unsync(&local->mc_list, &local->mc_count,
  361. &dev->mc_list, &dev->mc_count);
  362. spin_unlock_bh(&local->filter_lock);
  363. netif_addr_unlock_bh(dev);
  364. ieee80211_configure_filter(local);
  365. del_timer_sync(&local->dynamic_ps_timer);
  366. cancel_work_sync(&local->dynamic_ps_enable_work);
  367. /* APs need special treatment */
  368. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  369. struct ieee80211_sub_if_data *vlan, *tmpsdata;
  370. struct beacon_data *old_beacon = sdata->u.ap.beacon;
  371. /* remove beacon */
  372. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  373. synchronize_rcu();
  374. kfree(old_beacon);
  375. /* down all dependent devices, that is VLANs */
  376. list_for_each_entry_safe(vlan, tmpsdata, &sdata->u.ap.vlans,
  377. u.vlan.list)
  378. dev_close(vlan->dev);
  379. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  380. }
  381. local->open_count--;
  382. switch (sdata->vif.type) {
  383. case NL80211_IFTYPE_AP_VLAN:
  384. list_del(&sdata->u.vlan.list);
  385. /* no need to tell driver */
  386. break;
  387. case NL80211_IFTYPE_MONITOR:
  388. if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
  389. local->cooked_mntrs--;
  390. break;
  391. }
  392. local->monitors--;
  393. if (local->monitors == 0) {
  394. local->hw.conf.flags &= ~IEEE80211_CONF_MONITOR;
  395. hw_reconf_flags |= IEEE80211_CONF_CHANGE_MONITOR;
  396. }
  397. if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
  398. local->fif_fcsfail--;
  399. if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
  400. local->fif_plcpfail--;
  401. if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL) {
  402. local->fif_pspoll--;
  403. local->fif_control--;
  404. }
  405. if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
  406. local->fif_other_bss--;
  407. ieee80211_configure_filter(local);
  408. break;
  409. case NL80211_IFTYPE_STATION:
  410. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  411. del_timer_sync(&sdata->u.mgd.timer);
  412. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  413. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  414. /*
  415. * If any of the timers fired while we waited for it, it will
  416. * have queued its work. Now the work will be running again
  417. * but will not rearm the timer again because it checks
  418. * whether the interface is running, which, at this point,
  419. * it no longer is.
  420. */
  421. cancel_work_sync(&sdata->u.mgd.work);
  422. cancel_work_sync(&sdata->u.mgd.chswitch_work);
  423. cancel_work_sync(&sdata->u.mgd.monitor_work);
  424. cancel_work_sync(&sdata->u.mgd.beacon_loss_work);
  425. /*
  426. * When we get here, the interface is marked down.
  427. * Call synchronize_rcu() to wait for the RX path
  428. * should it be using the interface and enqueuing
  429. * frames at this very time on another CPU.
  430. */
  431. synchronize_rcu();
  432. skb_queue_purge(&sdata->u.mgd.skb_queue);
  433. /* fall through */
  434. case NL80211_IFTYPE_ADHOC:
  435. if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
  436. del_timer_sync(&sdata->u.ibss.timer);
  437. cancel_work_sync(&sdata->u.ibss.work);
  438. synchronize_rcu();
  439. skb_queue_purge(&sdata->u.ibss.skb_queue);
  440. }
  441. /* fall through */
  442. case NL80211_IFTYPE_MESH_POINT:
  443. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  444. /* other_bss and allmulti are always set on mesh
  445. * ifaces */
  446. local->fif_other_bss--;
  447. atomic_dec(&local->iff_allmultis);
  448. ieee80211_configure_filter(local);
  449. ieee80211_stop_mesh(sdata);
  450. }
  451. /* fall through */
  452. default:
  453. if (local->scan_sdata == sdata)
  454. ieee80211_scan_cancel(local);
  455. /*
  456. * Disable beaconing for AP and mesh, IBSS can't
  457. * still be joined to a network at this point.
  458. */
  459. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  460. sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
  461. ieee80211_bss_info_change_notify(sdata,
  462. BSS_CHANGED_BEACON_ENABLED);
  463. }
  464. /* disable all keys for as long as this netdev is down */
  465. ieee80211_disable_keys(sdata);
  466. drv_remove_interface(local, &sdata->vif);
  467. }
  468. sdata->bss = NULL;
  469. hw_reconf_flags |= __ieee80211_recalc_idle(local);
  470. ieee80211_recalc_ps(local, -1);
  471. if (local->open_count == 0) {
  472. ieee80211_clear_tx_pending(local);
  473. ieee80211_stop_device(local);
  474. /* no reconfiguring after stop! */
  475. hw_reconf_flags = 0;
  476. }
  477. /* do after stop to avoid reconfiguring when we stop anyway */
  478. if (hw_reconf_flags)
  479. ieee80211_hw_config(local, hw_reconf_flags);
  480. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  481. for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
  482. skb_queue_walk_safe(&local->pending[i], skb, tmp) {
  483. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  484. if (info->control.vif == &sdata->vif) {
  485. __skb_unlink(skb, &local->pending[i]);
  486. dev_kfree_skb_irq(skb);
  487. }
  488. }
  489. }
  490. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  491. return 0;
  492. }
  493. static void ieee80211_set_multicast_list(struct net_device *dev)
  494. {
  495. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  496. struct ieee80211_local *local = sdata->local;
  497. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  498. allmulti = !!(dev->flags & IFF_ALLMULTI);
  499. promisc = !!(dev->flags & IFF_PROMISC);
  500. sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
  501. sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
  502. if (allmulti != sdata_allmulti) {
  503. if (dev->flags & IFF_ALLMULTI)
  504. atomic_inc(&local->iff_allmultis);
  505. else
  506. atomic_dec(&local->iff_allmultis);
  507. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  508. }
  509. if (promisc != sdata_promisc) {
  510. if (dev->flags & IFF_PROMISC)
  511. atomic_inc(&local->iff_promiscs);
  512. else
  513. atomic_dec(&local->iff_promiscs);
  514. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  515. }
  516. spin_lock_bh(&local->filter_lock);
  517. __dev_addr_sync(&local->mc_list, &local->mc_count,
  518. &dev->mc_list, &dev->mc_count);
  519. spin_unlock_bh(&local->filter_lock);
  520. ieee80211_queue_work(&local->hw, &local->reconfig_filter);
  521. }
  522. /*
  523. * Called when the netdev is removed or, by the code below, before
  524. * the interface type changes.
  525. */
  526. static void ieee80211_teardown_sdata(struct net_device *dev)
  527. {
  528. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  529. struct ieee80211_local *local = sdata->local;
  530. struct beacon_data *beacon;
  531. struct sk_buff *skb;
  532. int flushed;
  533. int i;
  534. /* free extra data */
  535. ieee80211_free_keys(sdata);
  536. ieee80211_debugfs_remove_netdev(sdata);
  537. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  538. __skb_queue_purge(&sdata->fragments[i].skb_list);
  539. sdata->fragment_next = 0;
  540. switch (sdata->vif.type) {
  541. case NL80211_IFTYPE_AP:
  542. beacon = sdata->u.ap.beacon;
  543. rcu_assign_pointer(sdata->u.ap.beacon, NULL);
  544. synchronize_rcu();
  545. kfree(beacon);
  546. while ((skb = skb_dequeue(&sdata->u.ap.ps_bc_buf))) {
  547. local->total_ps_buffered--;
  548. dev_kfree_skb(skb);
  549. }
  550. break;
  551. case NL80211_IFTYPE_MESH_POINT:
  552. if (ieee80211_vif_is_mesh(&sdata->vif))
  553. mesh_rmc_free(sdata);
  554. break;
  555. case NL80211_IFTYPE_ADHOC:
  556. if (WARN_ON(sdata->u.ibss.presp))
  557. kfree_skb(sdata->u.ibss.presp);
  558. break;
  559. case NL80211_IFTYPE_STATION:
  560. case NL80211_IFTYPE_WDS:
  561. case NL80211_IFTYPE_AP_VLAN:
  562. case NL80211_IFTYPE_MONITOR:
  563. break;
  564. case NL80211_IFTYPE_UNSPECIFIED:
  565. case __NL80211_IFTYPE_AFTER_LAST:
  566. BUG();
  567. break;
  568. }
  569. flushed = sta_info_flush(local, sdata);
  570. WARN_ON(flushed);
  571. }
  572. static u16 ieee80211_netdev_select_queue(struct net_device *dev,
  573. struct sk_buff *skb)
  574. {
  575. return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
  576. }
  577. static const struct net_device_ops ieee80211_dataif_ops = {
  578. .ndo_open = ieee80211_open,
  579. .ndo_stop = ieee80211_stop,
  580. .ndo_uninit = ieee80211_teardown_sdata,
  581. .ndo_start_xmit = ieee80211_subif_start_xmit,
  582. .ndo_set_multicast_list = ieee80211_set_multicast_list,
  583. .ndo_change_mtu = ieee80211_change_mtu,
  584. .ndo_set_mac_address = ieee80211_change_mac,
  585. .ndo_select_queue = ieee80211_netdev_select_queue,
  586. };
  587. static u16 ieee80211_monitor_select_queue(struct net_device *dev,
  588. struct sk_buff *skb)
  589. {
  590. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  591. struct ieee80211_local *local = sdata->local;
  592. struct ieee80211_hdr *hdr;
  593. struct ieee80211_radiotap_header *rtap = (void *)skb->data;
  594. u8 *p;
  595. if (local->hw.queues < 4)
  596. return 0;
  597. if (skb->len < 4 ||
  598. skb->len < le16_to_cpu(rtap->it_len) + 2 /* frame control */)
  599. return 0; /* doesn't matter, frame will be dropped */
  600. hdr = (void *)((u8 *)skb->data + le16_to_cpu(rtap->it_len));
  601. if (!ieee80211_is_data(hdr->frame_control)) {
  602. skb->priority = 7;
  603. return ieee802_1d_to_ac[skb->priority];
  604. }
  605. if (!ieee80211_is_data_qos(hdr->frame_control)) {
  606. skb->priority = 0;
  607. return ieee802_1d_to_ac[skb->priority];
  608. }
  609. p = ieee80211_get_qos_ctl(hdr);
  610. skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
  611. return ieee80211_downgrade_queue(local, skb);
  612. }
  613. static const struct net_device_ops ieee80211_monitorif_ops = {
  614. .ndo_open = ieee80211_open,
  615. .ndo_stop = ieee80211_stop,
  616. .ndo_uninit = ieee80211_teardown_sdata,
  617. .ndo_start_xmit = ieee80211_monitor_start_xmit,
  618. .ndo_set_multicast_list = ieee80211_set_multicast_list,
  619. .ndo_change_mtu = ieee80211_change_mtu,
  620. .ndo_set_mac_address = eth_mac_addr,
  621. .ndo_select_queue = ieee80211_monitor_select_queue,
  622. };
  623. static void ieee80211_if_setup(struct net_device *dev)
  624. {
  625. ether_setup(dev);
  626. dev->netdev_ops = &ieee80211_dataif_ops;
  627. dev->destructor = free_netdev;
  628. }
  629. /*
  630. * Helper function to initialise an interface to a specific type.
  631. */
  632. static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
  633. enum nl80211_iftype type)
  634. {
  635. /* clear type-dependent union */
  636. memset(&sdata->u, 0, sizeof(sdata->u));
  637. /* and set some type-dependent values */
  638. sdata->vif.type = type;
  639. sdata->dev->netdev_ops = &ieee80211_dataif_ops;
  640. sdata->wdev.iftype = type;
  641. /* only monitor differs */
  642. sdata->dev->type = ARPHRD_ETHER;
  643. switch (type) {
  644. case NL80211_IFTYPE_AP:
  645. skb_queue_head_init(&sdata->u.ap.ps_bc_buf);
  646. INIT_LIST_HEAD(&sdata->u.ap.vlans);
  647. break;
  648. case NL80211_IFTYPE_STATION:
  649. ieee80211_sta_setup_sdata(sdata);
  650. break;
  651. case NL80211_IFTYPE_ADHOC:
  652. ieee80211_ibss_setup_sdata(sdata);
  653. break;
  654. case NL80211_IFTYPE_MESH_POINT:
  655. if (ieee80211_vif_is_mesh(&sdata->vif))
  656. ieee80211_mesh_init_sdata(sdata);
  657. break;
  658. case NL80211_IFTYPE_MONITOR:
  659. sdata->dev->type = ARPHRD_IEEE80211_RADIOTAP;
  660. sdata->dev->netdev_ops = &ieee80211_monitorif_ops;
  661. sdata->u.mntr_flags = MONITOR_FLAG_CONTROL |
  662. MONITOR_FLAG_OTHER_BSS;
  663. break;
  664. case NL80211_IFTYPE_WDS:
  665. case NL80211_IFTYPE_AP_VLAN:
  666. break;
  667. case NL80211_IFTYPE_UNSPECIFIED:
  668. case __NL80211_IFTYPE_AFTER_LAST:
  669. BUG();
  670. break;
  671. }
  672. ieee80211_debugfs_add_netdev(sdata);
  673. }
  674. int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
  675. enum nl80211_iftype type)
  676. {
  677. ASSERT_RTNL();
  678. if (type == sdata->vif.type)
  679. return 0;
  680. /* Setting ad-hoc mode on non-IBSS channel is not supported. */
  681. if (sdata->local->oper_channel->flags & IEEE80211_CHAN_NO_IBSS &&
  682. type == NL80211_IFTYPE_ADHOC)
  683. return -EOPNOTSUPP;
  684. /*
  685. * We could, here, on changes between IBSS/STA/MESH modes,
  686. * invoke an MLME function instead that disassociates etc.
  687. * and goes into the requested mode.
  688. */
  689. if (ieee80211_sdata_running(sdata))
  690. return -EBUSY;
  691. /* Purge and reset type-dependent state. */
  692. ieee80211_teardown_sdata(sdata->dev);
  693. ieee80211_setup_sdata(sdata, type);
  694. /* reset some values that shouldn't be kept across type changes */
  695. sdata->vif.bss_conf.basic_rates =
  696. ieee80211_mandatory_rates(sdata->local,
  697. sdata->local->hw.conf.channel->band);
  698. sdata->drop_unencrypted = 0;
  699. if (type == NL80211_IFTYPE_STATION)
  700. sdata->u.mgd.use_4addr = false;
  701. return 0;
  702. }
  703. static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
  704. struct net_device *dev,
  705. enum nl80211_iftype type)
  706. {
  707. struct ieee80211_sub_if_data *sdata;
  708. u64 mask, start, addr, val, inc;
  709. u8 *m;
  710. u8 tmp_addr[ETH_ALEN];
  711. int i;
  712. /* default ... something at least */
  713. memcpy(dev->perm_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  714. if (is_zero_ether_addr(local->hw.wiphy->addr_mask) &&
  715. local->hw.wiphy->n_addresses <= 1)
  716. return;
  717. mutex_lock(&local->iflist_mtx);
  718. switch (type) {
  719. case NL80211_IFTYPE_MONITOR:
  720. /* doesn't matter */
  721. break;
  722. case NL80211_IFTYPE_WDS:
  723. case NL80211_IFTYPE_AP_VLAN:
  724. /* match up with an AP interface */
  725. list_for_each_entry(sdata, &local->interfaces, list) {
  726. if (sdata->vif.type != NL80211_IFTYPE_AP)
  727. continue;
  728. memcpy(dev->perm_addr, sdata->vif.addr, ETH_ALEN);
  729. break;
  730. }
  731. /* keep default if no AP interface present */
  732. break;
  733. default:
  734. /* assign a new address if possible -- try n_addresses first */
  735. for (i = 0; i < local->hw.wiphy->n_addresses; i++) {
  736. bool used = false;
  737. list_for_each_entry(sdata, &local->interfaces, list) {
  738. if (memcmp(local->hw.wiphy->addresses[i].addr,
  739. sdata->vif.addr, ETH_ALEN) == 0) {
  740. used = true;
  741. break;
  742. }
  743. }
  744. if (!used) {
  745. memcpy(dev->perm_addr,
  746. local->hw.wiphy->addresses[i].addr,
  747. ETH_ALEN);
  748. break;
  749. }
  750. }
  751. /* try mask if available */
  752. if (is_zero_ether_addr(local->hw.wiphy->addr_mask))
  753. break;
  754. m = local->hw.wiphy->addr_mask;
  755. mask = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  756. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  757. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  758. if (__ffs64(mask) + hweight64(mask) != fls64(mask)) {
  759. /* not a contiguous mask ... not handled now! */
  760. printk(KERN_DEBUG "not contiguous\n");
  761. break;
  762. }
  763. m = local->hw.wiphy->perm_addr;
  764. start = ((u64)m[0] << 5*8) | ((u64)m[1] << 4*8) |
  765. ((u64)m[2] << 3*8) | ((u64)m[3] << 2*8) |
  766. ((u64)m[4] << 1*8) | ((u64)m[5] << 0*8);
  767. inc = 1ULL<<__ffs64(mask);
  768. val = (start & mask);
  769. addr = (start & ~mask) | (val & mask);
  770. do {
  771. bool used = false;
  772. tmp_addr[5] = addr >> 0*8;
  773. tmp_addr[4] = addr >> 1*8;
  774. tmp_addr[3] = addr >> 2*8;
  775. tmp_addr[2] = addr >> 3*8;
  776. tmp_addr[1] = addr >> 4*8;
  777. tmp_addr[0] = addr >> 5*8;
  778. val += inc;
  779. list_for_each_entry(sdata, &local->interfaces, list) {
  780. if (memcmp(tmp_addr, sdata->vif.addr,
  781. ETH_ALEN) == 0) {
  782. used = true;
  783. break;
  784. }
  785. }
  786. if (!used) {
  787. memcpy(dev->perm_addr, tmp_addr, ETH_ALEN);
  788. break;
  789. }
  790. addr = (start & ~mask) | (val & mask);
  791. } while (addr != start);
  792. break;
  793. }
  794. mutex_unlock(&local->iflist_mtx);
  795. }
  796. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  797. struct net_device **new_dev, enum nl80211_iftype type,
  798. struct vif_params *params)
  799. {
  800. struct net_device *ndev;
  801. struct ieee80211_sub_if_data *sdata = NULL;
  802. int ret, i;
  803. ASSERT_RTNL();
  804. ndev = alloc_netdev_mq(sizeof(*sdata) + local->hw.vif_data_size,
  805. name, ieee80211_if_setup, local->hw.queues);
  806. if (!ndev)
  807. return -ENOMEM;
  808. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  809. ndev->needed_headroom = local->tx_headroom +
  810. 4*6 /* four MAC addresses */
  811. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  812. + 6 /* mesh */
  813. + 8 /* rfc1042/bridge tunnel */
  814. - ETH_HLEN /* ethernet hard_header_len */
  815. + IEEE80211_ENCRYPT_HEADROOM;
  816. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  817. ret = dev_alloc_name(ndev, ndev->name);
  818. if (ret < 0)
  819. goto fail;
  820. ieee80211_assign_perm_addr(local, ndev, type);
  821. memcpy(ndev->dev_addr, ndev->perm_addr, ETH_ALEN);
  822. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  823. /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
  824. sdata = netdev_priv(ndev);
  825. ndev->ieee80211_ptr = &sdata->wdev;
  826. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  827. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  828. /* initialise type-independent data */
  829. sdata->wdev.wiphy = local->hw.wiphy;
  830. sdata->local = local;
  831. sdata->dev = ndev;
  832. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  833. skb_queue_head_init(&sdata->fragments[i].skb_list);
  834. INIT_LIST_HEAD(&sdata->key_list);
  835. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  836. struct ieee80211_supported_band *sband;
  837. sband = local->hw.wiphy->bands[i];
  838. sdata->rc_rateidx_mask[i] =
  839. sband ? (1 << sband->n_bitrates) - 1 : 0;
  840. }
  841. /* setup type-dependent data */
  842. ieee80211_setup_sdata(sdata, type);
  843. if (params) {
  844. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  845. if (type == NL80211_IFTYPE_STATION)
  846. sdata->u.mgd.use_4addr = params->use_4addr;
  847. }
  848. ret = register_netdevice(ndev);
  849. if (ret)
  850. goto fail;
  851. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  852. params && params->mesh_id_len)
  853. ieee80211_sdata_set_mesh_id(sdata,
  854. params->mesh_id_len,
  855. params->mesh_id);
  856. mutex_lock(&local->iflist_mtx);
  857. list_add_tail_rcu(&sdata->list, &local->interfaces);
  858. mutex_unlock(&local->iflist_mtx);
  859. if (new_dev)
  860. *new_dev = ndev;
  861. return 0;
  862. fail:
  863. free_netdev(ndev);
  864. return ret;
  865. }
  866. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  867. {
  868. ASSERT_RTNL();
  869. mutex_lock(&sdata->local->iflist_mtx);
  870. list_del_rcu(&sdata->list);
  871. mutex_unlock(&sdata->local->iflist_mtx);
  872. synchronize_rcu();
  873. unregister_netdevice(sdata->dev);
  874. }
  875. /*
  876. * Remove all interfaces, may only be called at hardware unregistration
  877. * time because it doesn't do RCU-safe list removals.
  878. */
  879. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  880. {
  881. struct ieee80211_sub_if_data *sdata, *tmp;
  882. LIST_HEAD(unreg_list);
  883. ASSERT_RTNL();
  884. mutex_lock(&local->iflist_mtx);
  885. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  886. list_del(&sdata->list);
  887. unregister_netdevice_queue(sdata->dev, &unreg_list);
  888. }
  889. mutex_unlock(&local->iflist_mtx);
  890. unregister_netdevice_many(&unreg_list);
  891. }
  892. static u32 ieee80211_idle_off(struct ieee80211_local *local,
  893. const char *reason)
  894. {
  895. if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
  896. return 0;
  897. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  898. printk(KERN_DEBUG "%s: device no longer idle - %s\n",
  899. wiphy_name(local->hw.wiphy), reason);
  900. #endif
  901. local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
  902. return IEEE80211_CONF_CHANGE_IDLE;
  903. }
  904. static u32 ieee80211_idle_on(struct ieee80211_local *local)
  905. {
  906. if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
  907. return 0;
  908. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  909. printk(KERN_DEBUG "%s: device now idle\n",
  910. wiphy_name(local->hw.wiphy));
  911. #endif
  912. drv_flush(local, false);
  913. local->hw.conf.flags |= IEEE80211_CONF_IDLE;
  914. return IEEE80211_CONF_CHANGE_IDLE;
  915. }
  916. u32 __ieee80211_recalc_idle(struct ieee80211_local *local)
  917. {
  918. struct ieee80211_sub_if_data *sdata;
  919. int count = 0;
  920. if (!list_empty(&local->work_list))
  921. return ieee80211_idle_off(local, "working");
  922. if (local->scanning)
  923. return ieee80211_idle_off(local, "scanning");
  924. list_for_each_entry(sdata, &local->interfaces, list) {
  925. if (!ieee80211_sdata_running(sdata))
  926. continue;
  927. /* do not count disabled managed interfaces */
  928. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  929. !sdata->u.mgd.associated)
  930. continue;
  931. /* do not count unused IBSS interfaces */
  932. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  933. !sdata->u.ibss.ssid_len)
  934. continue;
  935. /* count everything else */
  936. count++;
  937. }
  938. if (!count)
  939. return ieee80211_idle_on(local);
  940. else
  941. return ieee80211_idle_off(local, "in use");
  942. return 0;
  943. }
  944. void ieee80211_recalc_idle(struct ieee80211_local *local)
  945. {
  946. u32 chg;
  947. mutex_lock(&local->iflist_mtx);
  948. chg = __ieee80211_recalc_idle(local);
  949. mutex_unlock(&local->iflist_mtx);
  950. if (chg)
  951. ieee80211_hw_config(local, chg);
  952. }
  953. static int netdev_notify(struct notifier_block *nb,
  954. unsigned long state,
  955. void *ndev)
  956. {
  957. struct net_device *dev = ndev;
  958. struct ieee80211_sub_if_data *sdata;
  959. if (state != NETDEV_CHANGENAME)
  960. return 0;
  961. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  962. return 0;
  963. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  964. return 0;
  965. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  966. memcpy(sdata->name, dev->name, IFNAMSIZ);
  967. ieee80211_debugfs_rename_netdev(sdata);
  968. return 0;
  969. }
  970. static struct notifier_block mac80211_netdev_notifier = {
  971. .notifier_call = netdev_notify,
  972. };
  973. int ieee80211_iface_init(void)
  974. {
  975. return register_netdevice_notifier(&mac80211_netdev_notifier);
  976. }
  977. void ieee80211_iface_exit(void)
  978. {
  979. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  980. }