iface.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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. int ieee80211_if_add(struct ieee80211_local *local, const char *name,
  704. struct net_device **new_dev, enum nl80211_iftype type,
  705. struct vif_params *params)
  706. {
  707. struct net_device *ndev;
  708. struct ieee80211_sub_if_data *sdata = NULL;
  709. int ret, i;
  710. ASSERT_RTNL();
  711. ndev = alloc_netdev_mq(sizeof(*sdata) + local->hw.vif_data_size,
  712. name, ieee80211_if_setup, local->hw.queues);
  713. if (!ndev)
  714. return -ENOMEM;
  715. dev_net_set(ndev, wiphy_net(local->hw.wiphy));
  716. ndev->needed_headroom = local->tx_headroom +
  717. 4*6 /* four MAC addresses */
  718. + 2 + 2 + 2 + 2 /* ctl, dur, seq, qos */
  719. + 6 /* mesh */
  720. + 8 /* rfc1042/bridge tunnel */
  721. - ETH_HLEN /* ethernet hard_header_len */
  722. + IEEE80211_ENCRYPT_HEADROOM;
  723. ndev->needed_tailroom = IEEE80211_ENCRYPT_TAILROOM;
  724. ret = dev_alloc_name(ndev, ndev->name);
  725. if (ret < 0)
  726. goto fail;
  727. memcpy(ndev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  728. memcpy(ndev->perm_addr, ndev->dev_addr, ETH_ALEN);
  729. SET_NETDEV_DEV(ndev, wiphy_dev(local->hw.wiphy));
  730. /* don't use IEEE80211_DEV_TO_SUB_IF because it checks too much */
  731. sdata = netdev_priv(ndev);
  732. ndev->ieee80211_ptr = &sdata->wdev;
  733. memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
  734. memcpy(sdata->name, ndev->name, IFNAMSIZ);
  735. /* initialise type-independent data */
  736. sdata->wdev.wiphy = local->hw.wiphy;
  737. sdata->local = local;
  738. sdata->dev = ndev;
  739. for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
  740. skb_queue_head_init(&sdata->fragments[i].skb_list);
  741. INIT_LIST_HEAD(&sdata->key_list);
  742. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  743. struct ieee80211_supported_band *sband;
  744. sband = local->hw.wiphy->bands[i];
  745. sdata->rc_rateidx_mask[i] =
  746. sband ? (1 << sband->n_bitrates) - 1 : 0;
  747. }
  748. /* setup type-dependent data */
  749. ieee80211_setup_sdata(sdata, type);
  750. if (params) {
  751. ndev->ieee80211_ptr->use_4addr = params->use_4addr;
  752. if (type == NL80211_IFTYPE_STATION)
  753. sdata->u.mgd.use_4addr = params->use_4addr;
  754. }
  755. ret = register_netdevice(ndev);
  756. if (ret)
  757. goto fail;
  758. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  759. params && params->mesh_id_len)
  760. ieee80211_sdata_set_mesh_id(sdata,
  761. params->mesh_id_len,
  762. params->mesh_id);
  763. mutex_lock(&local->iflist_mtx);
  764. list_add_tail_rcu(&sdata->list, &local->interfaces);
  765. mutex_unlock(&local->iflist_mtx);
  766. if (new_dev)
  767. *new_dev = ndev;
  768. return 0;
  769. fail:
  770. free_netdev(ndev);
  771. return ret;
  772. }
  773. void ieee80211_if_remove(struct ieee80211_sub_if_data *sdata)
  774. {
  775. ASSERT_RTNL();
  776. mutex_lock(&sdata->local->iflist_mtx);
  777. list_del_rcu(&sdata->list);
  778. mutex_unlock(&sdata->local->iflist_mtx);
  779. synchronize_rcu();
  780. unregister_netdevice(sdata->dev);
  781. }
  782. /*
  783. * Remove all interfaces, may only be called at hardware unregistration
  784. * time because it doesn't do RCU-safe list removals.
  785. */
  786. void ieee80211_remove_interfaces(struct ieee80211_local *local)
  787. {
  788. struct ieee80211_sub_if_data *sdata, *tmp;
  789. LIST_HEAD(unreg_list);
  790. ASSERT_RTNL();
  791. mutex_lock(&local->iflist_mtx);
  792. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  793. list_del(&sdata->list);
  794. unregister_netdevice_queue(sdata->dev, &unreg_list);
  795. }
  796. mutex_unlock(&local->iflist_mtx);
  797. unregister_netdevice_many(&unreg_list);
  798. }
  799. static u32 ieee80211_idle_off(struct ieee80211_local *local,
  800. const char *reason)
  801. {
  802. if (!(local->hw.conf.flags & IEEE80211_CONF_IDLE))
  803. return 0;
  804. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  805. printk(KERN_DEBUG "%s: device no longer idle - %s\n",
  806. wiphy_name(local->hw.wiphy), reason);
  807. #endif
  808. local->hw.conf.flags &= ~IEEE80211_CONF_IDLE;
  809. return IEEE80211_CONF_CHANGE_IDLE;
  810. }
  811. static u32 ieee80211_idle_on(struct ieee80211_local *local)
  812. {
  813. if (local->hw.conf.flags & IEEE80211_CONF_IDLE)
  814. return 0;
  815. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  816. printk(KERN_DEBUG "%s: device now idle\n",
  817. wiphy_name(local->hw.wiphy));
  818. #endif
  819. drv_flush(local, false);
  820. local->hw.conf.flags |= IEEE80211_CONF_IDLE;
  821. return IEEE80211_CONF_CHANGE_IDLE;
  822. }
  823. u32 __ieee80211_recalc_idle(struct ieee80211_local *local)
  824. {
  825. struct ieee80211_sub_if_data *sdata;
  826. int count = 0;
  827. if (!list_empty(&local->work_list))
  828. return ieee80211_idle_off(local, "working");
  829. if (local->scanning)
  830. return ieee80211_idle_off(local, "scanning");
  831. list_for_each_entry(sdata, &local->interfaces, list) {
  832. if (!ieee80211_sdata_running(sdata))
  833. continue;
  834. /* do not count disabled managed interfaces */
  835. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  836. !sdata->u.mgd.associated)
  837. continue;
  838. /* do not count unused IBSS interfaces */
  839. if (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  840. !sdata->u.ibss.ssid_len)
  841. continue;
  842. /* count everything else */
  843. count++;
  844. }
  845. if (!count)
  846. return ieee80211_idle_on(local);
  847. else
  848. return ieee80211_idle_off(local, "in use");
  849. return 0;
  850. }
  851. void ieee80211_recalc_idle(struct ieee80211_local *local)
  852. {
  853. u32 chg;
  854. mutex_lock(&local->iflist_mtx);
  855. chg = __ieee80211_recalc_idle(local);
  856. mutex_unlock(&local->iflist_mtx);
  857. if (chg)
  858. ieee80211_hw_config(local, chg);
  859. }
  860. static int netdev_notify(struct notifier_block *nb,
  861. unsigned long state,
  862. void *ndev)
  863. {
  864. struct net_device *dev = ndev;
  865. struct ieee80211_sub_if_data *sdata;
  866. if (state != NETDEV_CHANGENAME)
  867. return 0;
  868. if (!dev->ieee80211_ptr || !dev->ieee80211_ptr->wiphy)
  869. return 0;
  870. if (dev->ieee80211_ptr->wiphy->privid != mac80211_wiphy_privid)
  871. return 0;
  872. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  873. memcpy(sdata->name, dev->name, IFNAMSIZ);
  874. ieee80211_debugfs_rename_netdev(sdata);
  875. return 0;
  876. }
  877. static struct notifier_block mac80211_netdev_notifier = {
  878. .notifier_call = netdev_notify,
  879. };
  880. int ieee80211_iface_init(void)
  881. {
  882. return register_netdevice_notifier(&mac80211_netdev_notifier);
  883. }
  884. void ieee80211_iface_exit(void)
  885. {
  886. unregister_netdevice_notifier(&mac80211_netdev_notifier);
  887. }