iface.c 30 KB

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