ieee80211.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <net/mac80211.h>
  11. #include <net/ieee80211_radiotap.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/if_arp.h>
  20. #include <linux/wireless.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/bitmap.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include "ieee80211_common.h"
  26. #include "ieee80211_i.h"
  27. #include "ieee80211_rate.h"
  28. #include "wep.h"
  29. #include "wme.h"
  30. #include "aes_ccm.h"
  31. #include "ieee80211_led.h"
  32. #include "cfg.h"
  33. #include "debugfs.h"
  34. #include "debugfs_netdev.h"
  35. /*
  36. * For seeing transmitted packets on monitor interfaces
  37. * we have a radiotap header too.
  38. */
  39. struct ieee80211_tx_status_rtap_hdr {
  40. struct ieee80211_radiotap_header hdr;
  41. __le16 tx_flags;
  42. u8 data_retries;
  43. } __attribute__ ((packed));
  44. /* common interface routines */
  45. static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
  46. {
  47. memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
  48. return ETH_ALEN;
  49. }
  50. /* must be called under mdev tx lock */
  51. static void ieee80211_configure_filter(struct ieee80211_local *local)
  52. {
  53. unsigned int changed_flags;
  54. unsigned int new_flags = 0;
  55. if (atomic_read(&local->iff_promiscs))
  56. new_flags |= FIF_PROMISC_IN_BSS;
  57. if (atomic_read(&local->iff_allmultis))
  58. new_flags |= FIF_ALLMULTI;
  59. if (local->monitors)
  60. new_flags |= FIF_CONTROL |
  61. FIF_OTHER_BSS |
  62. FIF_BCN_PRBRESP_PROMISC;
  63. changed_flags = local->filter_flags ^ new_flags;
  64. /* be a bit nasty */
  65. new_flags |= (1<<31);
  66. local->ops->configure_filter(local_to_hw(local),
  67. changed_flags, &new_flags,
  68. local->mdev->mc_count,
  69. local->mdev->mc_list);
  70. WARN_ON(new_flags & (1<<31));
  71. local->filter_flags = new_flags & ~(1<<31);
  72. }
  73. /* master interface */
  74. static int ieee80211_master_open(struct net_device *dev)
  75. {
  76. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  77. struct ieee80211_sub_if_data *sdata;
  78. int res = -EOPNOTSUPP;
  79. /* we hold the RTNL here so can safely walk the list */
  80. list_for_each_entry(sdata, &local->interfaces, list) {
  81. if (sdata->dev != dev && netif_running(sdata->dev)) {
  82. res = 0;
  83. break;
  84. }
  85. }
  86. return res;
  87. }
  88. static int ieee80211_master_stop(struct net_device *dev)
  89. {
  90. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  91. struct ieee80211_sub_if_data *sdata;
  92. /* we hold the RTNL here so can safely walk the list */
  93. list_for_each_entry(sdata, &local->interfaces, list)
  94. if (sdata->dev != dev && netif_running(sdata->dev))
  95. dev_close(sdata->dev);
  96. return 0;
  97. }
  98. static void ieee80211_master_set_multicast_list(struct net_device *dev)
  99. {
  100. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  101. ieee80211_configure_filter(local);
  102. }
  103. /* management interface */
  104. static void
  105. ieee80211_fill_frame_info(struct ieee80211_local *local,
  106. struct ieee80211_frame_info *fi,
  107. struct ieee80211_rx_status *status)
  108. {
  109. if (status) {
  110. struct timespec ts;
  111. struct ieee80211_rate *rate;
  112. jiffies_to_timespec(jiffies, &ts);
  113. fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
  114. ts.tv_nsec / 1000);
  115. fi->mactime = cpu_to_be64(status->mactime);
  116. switch (status->phymode) {
  117. case MODE_IEEE80211A:
  118. fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
  119. break;
  120. case MODE_IEEE80211B:
  121. fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
  122. break;
  123. case MODE_IEEE80211G:
  124. fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
  125. break;
  126. default:
  127. fi->phytype = htonl(0xAAAAAAAA);
  128. break;
  129. }
  130. fi->channel = htonl(status->channel);
  131. rate = ieee80211_get_rate(local, status->phymode,
  132. status->rate);
  133. if (rate) {
  134. fi->datarate = htonl(rate->rate);
  135. if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
  136. if (status->rate == rate->val)
  137. fi->preamble = htonl(2); /* long */
  138. else if (status->rate == rate->val2)
  139. fi->preamble = htonl(1); /* short */
  140. } else
  141. fi->preamble = htonl(0);
  142. } else {
  143. fi->datarate = htonl(0);
  144. fi->preamble = htonl(0);
  145. }
  146. fi->antenna = htonl(status->antenna);
  147. fi->priority = htonl(0xffffffff); /* no clue */
  148. fi->ssi_type = htonl(ieee80211_ssi_raw);
  149. fi->ssi_signal = htonl(status->ssi);
  150. fi->ssi_noise = 0x00000000;
  151. fi->encoding = 0;
  152. } else {
  153. /* clear everything because we really don't know.
  154. * the msg_type field isn't present on monitor frames
  155. * so we don't know whether it will be present or not,
  156. * but it's ok to not clear it since it'll be assigned
  157. * anyway */
  158. memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
  159. fi->ssi_type = htonl(ieee80211_ssi_none);
  160. }
  161. fi->version = htonl(IEEE80211_FI_VERSION);
  162. fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
  163. }
  164. /* this routine is actually not just for this, but also
  165. * for pushing fake 'management' frames into userspace.
  166. * it shall be replaced by a netlink-based system. */
  167. void
  168. ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
  169. struct ieee80211_rx_status *status, u32 msg_type)
  170. {
  171. struct ieee80211_frame_info *fi;
  172. const size_t hlen = sizeof(struct ieee80211_frame_info);
  173. struct net_device *dev = local->apdev;
  174. skb->dev = dev;
  175. if (skb_headroom(skb) < hlen) {
  176. I802_DEBUG_INC(local->rx_expand_skb_head);
  177. if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
  178. dev_kfree_skb(skb);
  179. return;
  180. }
  181. }
  182. fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
  183. ieee80211_fill_frame_info(local, fi, status);
  184. fi->msg_type = htonl(msg_type);
  185. dev->stats.rx_packets++;
  186. dev->stats.rx_bytes += skb->len;
  187. skb_set_mac_header(skb, 0);
  188. skb->ip_summed = CHECKSUM_UNNECESSARY;
  189. skb->pkt_type = PACKET_OTHERHOST;
  190. skb->protocol = htons(ETH_P_802_2);
  191. memset(skb->cb, 0, sizeof(skb->cb));
  192. netif_rx(skb);
  193. }
  194. static int ieee80211_mgmt_open(struct net_device *dev)
  195. {
  196. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  197. if (!netif_running(local->mdev))
  198. return -EOPNOTSUPP;
  199. return 0;
  200. }
  201. static int ieee80211_mgmt_stop(struct net_device *dev)
  202. {
  203. return 0;
  204. }
  205. static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
  206. {
  207. /* FIX: what would be proper limits for MTU?
  208. * This interface uses 802.11 frames. */
  209. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
  210. printk(KERN_WARNING "%s: invalid MTU %d\n",
  211. dev->name, new_mtu);
  212. return -EINVAL;
  213. }
  214. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  215. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  216. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  217. dev->mtu = new_mtu;
  218. return 0;
  219. }
  220. void ieee80211_if_mgmt_setup(struct net_device *dev)
  221. {
  222. ether_setup(dev);
  223. dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
  224. dev->change_mtu = ieee80211_change_mtu_apdev;
  225. dev->open = ieee80211_mgmt_open;
  226. dev->stop = ieee80211_mgmt_stop;
  227. dev->type = ARPHRD_IEEE80211_PRISM;
  228. dev->destructor = ieee80211_if_free;
  229. }
  230. /* regular interfaces */
  231. static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
  232. {
  233. /* FIX: what would be proper limits for MTU?
  234. * This interface uses 802.3 frames. */
  235. if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
  236. printk(KERN_WARNING "%s: invalid MTU %d\n",
  237. dev->name, new_mtu);
  238. return -EINVAL;
  239. }
  240. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  241. printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
  242. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  243. dev->mtu = new_mtu;
  244. return 0;
  245. }
  246. static inline int identical_mac_addr_allowed(int type1, int type2)
  247. {
  248. return (type1 == IEEE80211_IF_TYPE_MNTR ||
  249. type2 == IEEE80211_IF_TYPE_MNTR ||
  250. (type1 == IEEE80211_IF_TYPE_AP &&
  251. type2 == IEEE80211_IF_TYPE_WDS) ||
  252. (type1 == IEEE80211_IF_TYPE_WDS &&
  253. (type2 == IEEE80211_IF_TYPE_WDS ||
  254. type2 == IEEE80211_IF_TYPE_AP)) ||
  255. (type1 == IEEE80211_IF_TYPE_AP &&
  256. type2 == IEEE80211_IF_TYPE_VLAN) ||
  257. (type1 == IEEE80211_IF_TYPE_VLAN &&
  258. (type2 == IEEE80211_IF_TYPE_AP ||
  259. type2 == IEEE80211_IF_TYPE_VLAN)));
  260. }
  261. static int ieee80211_open(struct net_device *dev)
  262. {
  263. struct ieee80211_sub_if_data *sdata, *nsdata;
  264. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  265. struct ieee80211_if_init_conf conf;
  266. int res;
  267. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  268. /* we hold the RTNL here so can safely walk the list */
  269. list_for_each_entry(nsdata, &local->interfaces, list) {
  270. struct net_device *ndev = nsdata->dev;
  271. if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
  272. compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0) {
  273. /*
  274. * check whether it may have the same address
  275. */
  276. if (!identical_mac_addr_allowed(sdata->type,
  277. nsdata->type))
  278. return -ENOTUNIQ;
  279. /*
  280. * can only add VLANs to enabled APs
  281. */
  282. if (sdata->type == IEEE80211_IF_TYPE_VLAN &&
  283. nsdata->type == IEEE80211_IF_TYPE_AP &&
  284. netif_running(nsdata->dev))
  285. sdata->u.vlan.ap = nsdata;
  286. }
  287. }
  288. switch (sdata->type) {
  289. case IEEE80211_IF_TYPE_WDS:
  290. if (is_zero_ether_addr(sdata->u.wds.remote_addr))
  291. return -ENOLINK;
  292. break;
  293. case IEEE80211_IF_TYPE_VLAN:
  294. if (!sdata->u.vlan.ap)
  295. return -ENOLINK;
  296. break;
  297. case IEEE80211_IF_TYPE_AP:
  298. case IEEE80211_IF_TYPE_MGMT:
  299. case IEEE80211_IF_TYPE_STA:
  300. case IEEE80211_IF_TYPE_MNTR:
  301. case IEEE80211_IF_TYPE_IBSS:
  302. /* no special treatment */
  303. break;
  304. }
  305. if (local->open_count == 0) {
  306. res = 0;
  307. if (local->ops->start)
  308. res = local->ops->start(local_to_hw(local));
  309. if (res)
  310. return res;
  311. }
  312. switch (sdata->type) {
  313. case IEEE80211_IF_TYPE_VLAN:
  314. list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
  315. /* no need to tell driver */
  316. break;
  317. case IEEE80211_IF_TYPE_MNTR:
  318. /* must be before the call to ieee80211_configure_filter */
  319. local->monitors++;
  320. if (local->monitors == 1) {
  321. netif_tx_lock_bh(local->mdev);
  322. ieee80211_configure_filter(local);
  323. netif_tx_unlock_bh(local->mdev);
  324. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  325. ieee80211_hw_config(local);
  326. }
  327. break;
  328. case IEEE80211_IF_TYPE_STA:
  329. case IEEE80211_IF_TYPE_IBSS:
  330. sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  331. /* fall through */
  332. default:
  333. conf.if_id = dev->ifindex;
  334. conf.type = sdata->type;
  335. conf.mac_addr = dev->dev_addr;
  336. res = local->ops->add_interface(local_to_hw(local), &conf);
  337. if (res && !local->open_count && local->ops->stop)
  338. local->ops->stop(local_to_hw(local));
  339. if (res)
  340. return res;
  341. ieee80211_if_config(dev);
  342. ieee80211_reset_erp_info(dev);
  343. ieee80211_enable_keys(sdata);
  344. if (sdata->type == IEEE80211_IF_TYPE_STA &&
  345. !local->user_space_mlme)
  346. netif_carrier_off(dev);
  347. else
  348. netif_carrier_on(dev);
  349. }
  350. if (local->open_count == 0) {
  351. res = dev_open(local->mdev);
  352. WARN_ON(res);
  353. if (local->apdev) {
  354. res = dev_open(local->apdev);
  355. WARN_ON(res);
  356. }
  357. tasklet_enable(&local->tx_pending_tasklet);
  358. tasklet_enable(&local->tasklet);
  359. }
  360. local->open_count++;
  361. netif_start_queue(dev);
  362. return 0;
  363. }
  364. static int ieee80211_stop(struct net_device *dev)
  365. {
  366. struct ieee80211_sub_if_data *sdata;
  367. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  368. struct ieee80211_if_init_conf conf;
  369. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  370. netif_stop_queue(dev);
  371. dev_mc_unsync(local->mdev, dev);
  372. /* down all dependent devices, that is VLANs */
  373. if (sdata->type == IEEE80211_IF_TYPE_AP) {
  374. struct ieee80211_sub_if_data *vlan, *tmp;
  375. list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
  376. u.vlan.list)
  377. dev_close(vlan->dev);
  378. WARN_ON(!list_empty(&sdata->u.ap.vlans));
  379. }
  380. local->open_count--;
  381. switch (sdata->type) {
  382. case IEEE80211_IF_TYPE_VLAN:
  383. list_del(&sdata->u.vlan.list);
  384. sdata->u.vlan.ap = NULL;
  385. /* no need to tell driver */
  386. break;
  387. case IEEE80211_IF_TYPE_MNTR:
  388. local->monitors--;
  389. if (local->monitors == 0) {
  390. netif_tx_lock_bh(local->mdev);
  391. ieee80211_configure_filter(local);
  392. netif_tx_unlock_bh(local->mdev);
  393. local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
  394. ieee80211_hw_config(local);
  395. }
  396. break;
  397. case IEEE80211_IF_TYPE_STA:
  398. case IEEE80211_IF_TYPE_IBSS:
  399. sdata->u.sta.state = IEEE80211_DISABLED;
  400. del_timer_sync(&sdata->u.sta.timer);
  401. /*
  402. * When we get here, the interface is marked down.
  403. * Call synchronize_rcu() to wait for the RX path
  404. * should it be using the interface and enqueuing
  405. * frames at this very time on another CPU.
  406. */
  407. synchronize_rcu();
  408. skb_queue_purge(&sdata->u.sta.skb_queue);
  409. if (!local->ops->hw_scan &&
  410. local->scan_dev == sdata->dev) {
  411. local->sta_scanning = 0;
  412. cancel_delayed_work(&local->scan_work);
  413. }
  414. flush_workqueue(local->hw.workqueue);
  415. /* fall through */
  416. default:
  417. conf.if_id = dev->ifindex;
  418. conf.type = sdata->type;
  419. conf.mac_addr = dev->dev_addr;
  420. /* disable all keys for as long as this netdev is down */
  421. ieee80211_disable_keys(sdata);
  422. local->ops->remove_interface(local_to_hw(local), &conf);
  423. }
  424. if (local->open_count == 0) {
  425. if (netif_running(local->mdev))
  426. dev_close(local->mdev);
  427. if (local->apdev)
  428. dev_close(local->apdev);
  429. if (local->ops->stop)
  430. local->ops->stop(local_to_hw(local));
  431. tasklet_disable(&local->tx_pending_tasklet);
  432. tasklet_disable(&local->tasklet);
  433. }
  434. return 0;
  435. }
  436. static void ieee80211_set_multicast_list(struct net_device *dev)
  437. {
  438. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  439. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  440. int allmulti, promisc, sdata_allmulti, sdata_promisc;
  441. allmulti = !!(dev->flags & IFF_ALLMULTI);
  442. promisc = !!(dev->flags & IFF_PROMISC);
  443. sdata_allmulti = sdata->flags & IEEE80211_SDATA_ALLMULTI;
  444. sdata_promisc = sdata->flags & IEEE80211_SDATA_PROMISC;
  445. if (allmulti != sdata_allmulti) {
  446. if (dev->flags & IFF_ALLMULTI)
  447. atomic_inc(&local->iff_allmultis);
  448. else
  449. atomic_dec(&local->iff_allmultis);
  450. sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
  451. }
  452. if (promisc != sdata_promisc) {
  453. if (dev->flags & IFF_PROMISC)
  454. atomic_inc(&local->iff_promiscs);
  455. else
  456. atomic_dec(&local->iff_promiscs);
  457. sdata->flags ^= IEEE80211_SDATA_PROMISC;
  458. }
  459. dev_mc_sync(local->mdev, dev);
  460. }
  461. static const struct header_ops ieee80211_header_ops = {
  462. .create = eth_header,
  463. .parse = header_parse_80211,
  464. .rebuild = eth_rebuild_header,
  465. .cache = eth_header_cache,
  466. .cache_update = eth_header_cache_update,
  467. };
  468. /* Must not be called for mdev and apdev */
  469. void ieee80211_if_setup(struct net_device *dev)
  470. {
  471. ether_setup(dev);
  472. dev->header_ops = &ieee80211_header_ops;
  473. dev->hard_start_xmit = ieee80211_subif_start_xmit;
  474. dev->wireless_handlers = &ieee80211_iw_handler_def;
  475. dev->set_multicast_list = ieee80211_set_multicast_list;
  476. dev->change_mtu = ieee80211_change_mtu;
  477. dev->open = ieee80211_open;
  478. dev->stop = ieee80211_stop;
  479. dev->destructor = ieee80211_if_free;
  480. }
  481. /* WDS specialties */
  482. int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
  483. {
  484. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  485. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  486. struct sta_info *sta;
  487. DECLARE_MAC_BUF(mac);
  488. if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
  489. return 0;
  490. /* Create STA entry for the new peer */
  491. sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
  492. if (!sta)
  493. return -ENOMEM;
  494. sta_info_put(sta);
  495. /* Remove STA entry for the old peer */
  496. sta = sta_info_get(local, sdata->u.wds.remote_addr);
  497. if (sta) {
  498. sta_info_free(sta);
  499. sta_info_put(sta);
  500. } else {
  501. printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
  502. "peer %s\n",
  503. dev->name, print_mac(mac, sdata->u.wds.remote_addr));
  504. }
  505. /* Update WDS link data */
  506. memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
  507. return 0;
  508. }
  509. /* everything else */
  510. static int __ieee80211_if_config(struct net_device *dev,
  511. struct sk_buff *beacon,
  512. struct ieee80211_tx_control *control)
  513. {
  514. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  515. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  516. struct ieee80211_if_conf conf;
  517. if (!local->ops->config_interface || !netif_running(dev))
  518. return 0;
  519. memset(&conf, 0, sizeof(conf));
  520. conf.type = sdata->type;
  521. if (sdata->type == IEEE80211_IF_TYPE_STA ||
  522. sdata->type == IEEE80211_IF_TYPE_IBSS) {
  523. conf.bssid = sdata->u.sta.bssid;
  524. conf.ssid = sdata->u.sta.ssid;
  525. conf.ssid_len = sdata->u.sta.ssid_len;
  526. conf.generic_elem = sdata->u.sta.extra_ie;
  527. conf.generic_elem_len = sdata->u.sta.extra_ie_len;
  528. } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
  529. conf.ssid = sdata->u.ap.ssid;
  530. conf.ssid_len = sdata->u.ap.ssid_len;
  531. conf.generic_elem = sdata->u.ap.generic_elem;
  532. conf.generic_elem_len = sdata->u.ap.generic_elem_len;
  533. conf.beacon = beacon;
  534. conf.beacon_control = control;
  535. }
  536. return local->ops->config_interface(local_to_hw(local),
  537. dev->ifindex, &conf);
  538. }
  539. int ieee80211_if_config(struct net_device *dev)
  540. {
  541. return __ieee80211_if_config(dev, NULL, NULL);
  542. }
  543. int ieee80211_if_config_beacon(struct net_device *dev)
  544. {
  545. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  546. struct ieee80211_tx_control control;
  547. struct sk_buff *skb;
  548. if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
  549. return 0;
  550. skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
  551. if (!skb)
  552. return -ENOMEM;
  553. return __ieee80211_if_config(dev, skb, &control);
  554. }
  555. int ieee80211_hw_config(struct ieee80211_local *local)
  556. {
  557. struct ieee80211_hw_mode *mode;
  558. struct ieee80211_channel *chan;
  559. int ret = 0;
  560. if (local->sta_scanning) {
  561. chan = local->scan_channel;
  562. mode = local->scan_hw_mode;
  563. } else {
  564. chan = local->oper_channel;
  565. mode = local->oper_hw_mode;
  566. }
  567. local->hw.conf.channel = chan->chan;
  568. local->hw.conf.channel_val = chan->val;
  569. if (!local->hw.conf.power_level) {
  570. local->hw.conf.power_level = chan->power_level;
  571. } else {
  572. local->hw.conf.power_level = min(chan->power_level,
  573. local->hw.conf.power_level);
  574. }
  575. local->hw.conf.freq = chan->freq;
  576. local->hw.conf.phymode = mode->mode;
  577. local->hw.conf.antenna_max = chan->antenna_max;
  578. local->hw.conf.chan = chan;
  579. local->hw.conf.mode = mode;
  580. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  581. printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
  582. "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
  583. local->hw.conf.phymode);
  584. #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
  585. if (local->ops->config)
  586. ret = local->ops->config(local_to_hw(local), &local->hw.conf);
  587. return ret;
  588. }
  589. void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
  590. {
  591. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  592. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  593. if (local->ops->erp_ie_changed)
  594. local->ops->erp_ie_changed(local_to_hw(local), changes,
  595. !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
  596. !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
  597. }
  598. void ieee80211_reset_erp_info(struct net_device *dev)
  599. {
  600. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  601. sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
  602. IEEE80211_SDATA_SHORT_PREAMBLE);
  603. ieee80211_erp_info_change_notify(dev,
  604. IEEE80211_ERP_CHANGE_PROTECTION |
  605. IEEE80211_ERP_CHANGE_PREAMBLE);
  606. }
  607. void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
  608. struct sk_buff *skb,
  609. struct ieee80211_tx_status *status)
  610. {
  611. struct ieee80211_local *local = hw_to_local(hw);
  612. struct ieee80211_tx_status *saved;
  613. int tmp;
  614. skb->dev = local->mdev;
  615. saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
  616. if (unlikely(!saved)) {
  617. if (net_ratelimit())
  618. printk(KERN_WARNING "%s: Not enough memory, "
  619. "dropping tx status", skb->dev->name);
  620. /* should be dev_kfree_skb_irq, but due to this function being
  621. * named _irqsafe instead of just _irq we can't be sure that
  622. * people won't call it from non-irq contexts */
  623. dev_kfree_skb_any(skb);
  624. return;
  625. }
  626. memcpy(saved, status, sizeof(struct ieee80211_tx_status));
  627. /* copy pointer to saved status into skb->cb for use by tasklet */
  628. memcpy(skb->cb, &saved, sizeof(saved));
  629. skb->pkt_type = IEEE80211_TX_STATUS_MSG;
  630. skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
  631. &local->skb_queue : &local->skb_queue_unreliable, skb);
  632. tmp = skb_queue_len(&local->skb_queue) +
  633. skb_queue_len(&local->skb_queue_unreliable);
  634. while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
  635. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  636. memcpy(&saved, skb->cb, sizeof(saved));
  637. kfree(saved);
  638. dev_kfree_skb_irq(skb);
  639. tmp--;
  640. I802_DEBUG_INC(local->tx_status_drop);
  641. }
  642. tasklet_schedule(&local->tasklet);
  643. }
  644. EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
  645. static void ieee80211_tasklet_handler(unsigned long data)
  646. {
  647. struct ieee80211_local *local = (struct ieee80211_local *) data;
  648. struct sk_buff *skb;
  649. struct ieee80211_rx_status rx_status;
  650. struct ieee80211_tx_status *tx_status;
  651. while ((skb = skb_dequeue(&local->skb_queue)) ||
  652. (skb = skb_dequeue(&local->skb_queue_unreliable))) {
  653. switch (skb->pkt_type) {
  654. case IEEE80211_RX_MSG:
  655. /* status is in skb->cb */
  656. memcpy(&rx_status, skb->cb, sizeof(rx_status));
  657. /* Clear skb->type in order to not confuse kernel
  658. * netstack. */
  659. skb->pkt_type = 0;
  660. __ieee80211_rx(local_to_hw(local), skb, &rx_status);
  661. break;
  662. case IEEE80211_TX_STATUS_MSG:
  663. /* get pointer to saved status out of skb->cb */
  664. memcpy(&tx_status, skb->cb, sizeof(tx_status));
  665. skb->pkt_type = 0;
  666. ieee80211_tx_status(local_to_hw(local),
  667. skb, tx_status);
  668. kfree(tx_status);
  669. break;
  670. default: /* should never get here! */
  671. printk(KERN_ERR "%s: Unknown message type (%d)\n",
  672. wiphy_name(local->hw.wiphy), skb->pkt_type);
  673. dev_kfree_skb(skb);
  674. break;
  675. }
  676. }
  677. }
  678. /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
  679. * make a prepared TX frame (one that has been given to hw) to look like brand
  680. * new IEEE 802.11 frame that is ready to go through TX processing again.
  681. * Also, tx_packet_data in cb is restored from tx_control. */
  682. static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
  683. struct ieee80211_key *key,
  684. struct sk_buff *skb,
  685. struct ieee80211_tx_control *control)
  686. {
  687. int hdrlen, iv_len, mic_len;
  688. struct ieee80211_tx_packet_data *pkt_data;
  689. pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
  690. pkt_data->ifindex = control->ifindex;
  691. pkt_data->flags = 0;
  692. if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
  693. pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
  694. if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
  695. pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
  696. if (control->flags & IEEE80211_TXCTL_REQUEUE)
  697. pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
  698. if (control->type == IEEE80211_IF_TYPE_MGMT)
  699. pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
  700. pkt_data->queue = control->queue;
  701. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  702. if (!key)
  703. goto no_key;
  704. switch (key->conf.alg) {
  705. case ALG_WEP:
  706. iv_len = WEP_IV_LEN;
  707. mic_len = WEP_ICV_LEN;
  708. break;
  709. case ALG_TKIP:
  710. iv_len = TKIP_IV_LEN;
  711. mic_len = TKIP_ICV_LEN;
  712. break;
  713. case ALG_CCMP:
  714. iv_len = CCMP_HDR_LEN;
  715. mic_len = CCMP_MIC_LEN;
  716. break;
  717. default:
  718. goto no_key;
  719. }
  720. if (skb->len >= mic_len &&
  721. !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
  722. skb_trim(skb, skb->len - mic_len);
  723. if (skb->len >= iv_len && skb->len > hdrlen) {
  724. memmove(skb->data + iv_len, skb->data, hdrlen);
  725. skb_pull(skb, iv_len);
  726. }
  727. no_key:
  728. {
  729. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  730. u16 fc = le16_to_cpu(hdr->frame_control);
  731. if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
  732. fc &= ~IEEE80211_STYPE_QOS_DATA;
  733. hdr->frame_control = cpu_to_le16(fc);
  734. memmove(skb->data + 2, skb->data, hdrlen - 2);
  735. skb_pull(skb, 2);
  736. }
  737. }
  738. }
  739. void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  740. struct ieee80211_tx_status *status)
  741. {
  742. struct sk_buff *skb2;
  743. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  744. struct ieee80211_local *local = hw_to_local(hw);
  745. u16 frag, type;
  746. u32 msg_type;
  747. struct ieee80211_tx_status_rtap_hdr *rthdr;
  748. struct ieee80211_sub_if_data *sdata;
  749. int monitors;
  750. if (!status) {
  751. printk(KERN_ERR
  752. "%s: ieee80211_tx_status called with NULL status\n",
  753. wiphy_name(local->hw.wiphy));
  754. dev_kfree_skb(skb);
  755. return;
  756. }
  757. if (status->excessive_retries) {
  758. struct sta_info *sta;
  759. sta = sta_info_get(local, hdr->addr1);
  760. if (sta) {
  761. if (sta->flags & WLAN_STA_PS) {
  762. /* The STA is in power save mode, so assume
  763. * that this TX packet failed because of that.
  764. */
  765. status->excessive_retries = 0;
  766. status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
  767. }
  768. sta_info_put(sta);
  769. }
  770. }
  771. if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
  772. struct sta_info *sta;
  773. sta = sta_info_get(local, hdr->addr1);
  774. if (sta) {
  775. sta->tx_filtered_count++;
  776. /* Clear the TX filter mask for this STA when sending
  777. * the next packet. If the STA went to power save mode,
  778. * this will happen when it is waking up for the next
  779. * time. */
  780. sta->clear_dst_mask = 1;
  781. /* TODO: Is the WLAN_STA_PS flag always set here or is
  782. * the race between RX and TX status causing some
  783. * packets to be filtered out before 80211.o gets an
  784. * update for PS status? This seems to be the case, so
  785. * no changes are likely to be needed. */
  786. if (sta->flags & WLAN_STA_PS &&
  787. skb_queue_len(&sta->tx_filtered) <
  788. STA_MAX_TX_BUFFER) {
  789. ieee80211_remove_tx_extra(local, sta->key,
  790. skb,
  791. &status->control);
  792. skb_queue_tail(&sta->tx_filtered, skb);
  793. } else if (!(sta->flags & WLAN_STA_PS) &&
  794. !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
  795. /* Software retry the packet once */
  796. status->control.flags |= IEEE80211_TXCTL_REQUEUE;
  797. ieee80211_remove_tx_extra(local, sta->key,
  798. skb,
  799. &status->control);
  800. dev_queue_xmit(skb);
  801. } else {
  802. if (net_ratelimit()) {
  803. printk(KERN_DEBUG "%s: dropped TX "
  804. "filtered frame queue_len=%d "
  805. "PS=%d @%lu\n",
  806. wiphy_name(local->hw.wiphy),
  807. skb_queue_len(
  808. &sta->tx_filtered),
  809. !!(sta->flags & WLAN_STA_PS),
  810. jiffies);
  811. }
  812. dev_kfree_skb(skb);
  813. }
  814. sta_info_put(sta);
  815. return;
  816. }
  817. } else {
  818. /* FIXME: STUPID to call this with both local and local->mdev */
  819. rate_control_tx_status(local, local->mdev, skb, status);
  820. }
  821. ieee80211_led_tx(local, 0);
  822. /* SNMP counters
  823. * Fragments are passed to low-level drivers as separate skbs, so these
  824. * are actually fragments, not frames. Update frame counters only for
  825. * the first fragment of the frame. */
  826. frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
  827. type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
  828. if (status->flags & IEEE80211_TX_STATUS_ACK) {
  829. if (frag == 0) {
  830. local->dot11TransmittedFrameCount++;
  831. if (is_multicast_ether_addr(hdr->addr1))
  832. local->dot11MulticastTransmittedFrameCount++;
  833. if (status->retry_count > 0)
  834. local->dot11RetryCount++;
  835. if (status->retry_count > 1)
  836. local->dot11MultipleRetryCount++;
  837. }
  838. /* This counter shall be incremented for an acknowledged MPDU
  839. * with an individual address in the address 1 field or an MPDU
  840. * with a multicast address in the address 1 field of type Data
  841. * or Management. */
  842. if (!is_multicast_ether_addr(hdr->addr1) ||
  843. type == IEEE80211_FTYPE_DATA ||
  844. type == IEEE80211_FTYPE_MGMT)
  845. local->dot11TransmittedFragmentCount++;
  846. } else {
  847. if (frag == 0)
  848. local->dot11FailedCount++;
  849. }
  850. msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
  851. ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
  852. /* this was a transmitted frame, but now we want to reuse it */
  853. skb_orphan(skb);
  854. if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
  855. local->apdev) {
  856. if (local->monitors) {
  857. skb2 = skb_clone(skb, GFP_ATOMIC);
  858. } else {
  859. skb2 = skb;
  860. skb = NULL;
  861. }
  862. if (skb2)
  863. /* Send frame to hostapd */
  864. ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
  865. if (!skb)
  866. return;
  867. }
  868. if (!local->monitors) {
  869. dev_kfree_skb(skb);
  870. return;
  871. }
  872. /* send frame to monitor interfaces now */
  873. if (skb_headroom(skb) < sizeof(*rthdr)) {
  874. printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
  875. dev_kfree_skb(skb);
  876. return;
  877. }
  878. rthdr = (struct ieee80211_tx_status_rtap_hdr*)
  879. skb_push(skb, sizeof(*rthdr));
  880. memset(rthdr, 0, sizeof(*rthdr));
  881. rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
  882. rthdr->hdr.it_present =
  883. cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
  884. (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
  885. if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
  886. !is_multicast_ether_addr(hdr->addr1))
  887. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
  888. if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
  889. (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
  890. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
  891. else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
  892. rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
  893. rthdr->data_retries = status->retry_count;
  894. rcu_read_lock();
  895. monitors = local->monitors;
  896. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  897. /*
  898. * Using the monitors counter is possibly racy, but
  899. * if the value is wrong we simply either clone the skb
  900. * once too much or forget sending it to one monitor iface
  901. * The latter case isn't nice but fixing the race is much
  902. * more complicated.
  903. */
  904. if (!monitors || !skb)
  905. goto out;
  906. if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
  907. if (!netif_running(sdata->dev))
  908. continue;
  909. monitors--;
  910. if (monitors)
  911. skb2 = skb_clone(skb, GFP_ATOMIC);
  912. else
  913. skb2 = NULL;
  914. skb->dev = sdata->dev;
  915. /* XXX: is this sufficient for BPF? */
  916. skb_set_mac_header(skb, 0);
  917. skb->ip_summed = CHECKSUM_UNNECESSARY;
  918. skb->pkt_type = PACKET_OTHERHOST;
  919. skb->protocol = htons(ETH_P_802_2);
  920. memset(skb->cb, 0, sizeof(skb->cb));
  921. netif_rx(skb);
  922. skb = skb2;
  923. }
  924. }
  925. out:
  926. rcu_read_unlock();
  927. if (skb)
  928. dev_kfree_skb(skb);
  929. }
  930. EXPORT_SYMBOL(ieee80211_tx_status);
  931. struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
  932. const struct ieee80211_ops *ops)
  933. {
  934. struct net_device *mdev;
  935. struct ieee80211_local *local;
  936. struct ieee80211_sub_if_data *sdata;
  937. int priv_size;
  938. struct wiphy *wiphy;
  939. /* Ensure 32-byte alignment of our private data and hw private data.
  940. * We use the wiphy priv data for both our ieee80211_local and for
  941. * the driver's private data
  942. *
  943. * In memory it'll be like this:
  944. *
  945. * +-------------------------+
  946. * | struct wiphy |
  947. * +-------------------------+
  948. * | struct ieee80211_local |
  949. * +-------------------------+
  950. * | driver's private data |
  951. * +-------------------------+
  952. *
  953. */
  954. priv_size = ((sizeof(struct ieee80211_local) +
  955. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
  956. priv_data_len;
  957. wiphy = wiphy_new(&mac80211_config_ops, priv_size);
  958. if (!wiphy)
  959. return NULL;
  960. wiphy->privid = mac80211_wiphy_privid;
  961. local = wiphy_priv(wiphy);
  962. local->hw.wiphy = wiphy;
  963. local->hw.priv = (char *)local +
  964. ((sizeof(struct ieee80211_local) +
  965. NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
  966. BUG_ON(!ops->tx);
  967. BUG_ON(!ops->start);
  968. BUG_ON(!ops->stop);
  969. BUG_ON(!ops->config);
  970. BUG_ON(!ops->add_interface);
  971. BUG_ON(!ops->remove_interface);
  972. BUG_ON(!ops->configure_filter);
  973. local->ops = ops;
  974. /* for now, mdev needs sub_if_data :/ */
  975. mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
  976. "wmaster%d", ether_setup);
  977. if (!mdev) {
  978. wiphy_free(wiphy);
  979. return NULL;
  980. }
  981. sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
  982. mdev->ieee80211_ptr = &sdata->wdev;
  983. sdata->wdev.wiphy = wiphy;
  984. local->hw.queues = 1; /* default */
  985. local->mdev = mdev;
  986. local->rx_pre_handlers = ieee80211_rx_pre_handlers;
  987. local->rx_handlers = ieee80211_rx_handlers;
  988. local->tx_handlers = ieee80211_tx_handlers;
  989. local->bridge_packets = 1;
  990. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  991. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  992. local->short_retry_limit = 7;
  993. local->long_retry_limit = 4;
  994. local->hw.conf.radio_enabled = 1;
  995. local->enabled_modes = ~0;
  996. INIT_LIST_HEAD(&local->modes_list);
  997. INIT_LIST_HEAD(&local->interfaces);
  998. INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
  999. ieee80211_rx_bss_list_init(mdev);
  1000. sta_info_init(local);
  1001. mdev->hard_start_xmit = ieee80211_master_start_xmit;
  1002. mdev->open = ieee80211_master_open;
  1003. mdev->stop = ieee80211_master_stop;
  1004. mdev->type = ARPHRD_IEEE80211;
  1005. mdev->header_ops = &ieee80211_header_ops;
  1006. mdev->set_multicast_list = ieee80211_master_set_multicast_list;
  1007. sdata->type = IEEE80211_IF_TYPE_AP;
  1008. sdata->dev = mdev;
  1009. sdata->local = local;
  1010. sdata->u.ap.force_unicast_rateidx = -1;
  1011. sdata->u.ap.max_ratectrl_rateidx = -1;
  1012. ieee80211_if_sdata_init(sdata);
  1013. /* no RCU needed since we're still during init phase */
  1014. list_add_tail(&sdata->list, &local->interfaces);
  1015. tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
  1016. (unsigned long)local);
  1017. tasklet_disable(&local->tx_pending_tasklet);
  1018. tasklet_init(&local->tasklet,
  1019. ieee80211_tasklet_handler,
  1020. (unsigned long) local);
  1021. tasklet_disable(&local->tasklet);
  1022. skb_queue_head_init(&local->skb_queue);
  1023. skb_queue_head_init(&local->skb_queue_unreliable);
  1024. return local_to_hw(local);
  1025. }
  1026. EXPORT_SYMBOL(ieee80211_alloc_hw);
  1027. int ieee80211_register_hw(struct ieee80211_hw *hw)
  1028. {
  1029. struct ieee80211_local *local = hw_to_local(hw);
  1030. const char *name;
  1031. int result;
  1032. result = wiphy_register(local->hw.wiphy);
  1033. if (result < 0)
  1034. return result;
  1035. name = wiphy_dev(local->hw.wiphy)->driver->name;
  1036. local->hw.workqueue = create_singlethread_workqueue(name);
  1037. if (!local->hw.workqueue) {
  1038. result = -ENOMEM;
  1039. goto fail_workqueue;
  1040. }
  1041. /*
  1042. * The hardware needs headroom for sending the frame,
  1043. * and we need some headroom for passing the frame to monitor
  1044. * interfaces, but never both at the same time.
  1045. */
  1046. local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
  1047. sizeof(struct ieee80211_tx_status_rtap_hdr));
  1048. debugfs_hw_add(local);
  1049. local->hw.conf.beacon_int = 1000;
  1050. local->wstats_flags |= local->hw.max_rssi ?
  1051. IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
  1052. local->wstats_flags |= local->hw.max_signal ?
  1053. IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
  1054. local->wstats_flags |= local->hw.max_noise ?
  1055. IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
  1056. if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
  1057. local->wstats_flags |= IW_QUAL_DBM;
  1058. result = sta_info_start(local);
  1059. if (result < 0)
  1060. goto fail_sta_info;
  1061. rtnl_lock();
  1062. result = dev_alloc_name(local->mdev, local->mdev->name);
  1063. if (result < 0)
  1064. goto fail_dev;
  1065. memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
  1066. SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
  1067. result = register_netdevice(local->mdev);
  1068. if (result < 0)
  1069. goto fail_dev;
  1070. ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1071. ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
  1072. result = ieee80211_init_rate_ctrl_alg(local, NULL);
  1073. if (result < 0) {
  1074. printk(KERN_DEBUG "%s: Failed to initialize rate control "
  1075. "algorithm\n", wiphy_name(local->hw.wiphy));
  1076. goto fail_rate;
  1077. }
  1078. result = ieee80211_wep_init(local);
  1079. if (result < 0) {
  1080. printk(KERN_DEBUG "%s: Failed to initialize wep\n",
  1081. wiphy_name(local->hw.wiphy));
  1082. goto fail_wep;
  1083. }
  1084. ieee80211_install_qdisc(local->mdev);
  1085. /* add one default STA interface */
  1086. result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
  1087. IEEE80211_IF_TYPE_STA);
  1088. if (result)
  1089. printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
  1090. wiphy_name(local->hw.wiphy));
  1091. local->reg_state = IEEE80211_DEV_REGISTERED;
  1092. rtnl_unlock();
  1093. ieee80211_led_init(local);
  1094. return 0;
  1095. fail_wep:
  1096. rate_control_deinitialize(local);
  1097. fail_rate:
  1098. ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1099. unregister_netdevice(local->mdev);
  1100. fail_dev:
  1101. rtnl_unlock();
  1102. sta_info_stop(local);
  1103. fail_sta_info:
  1104. debugfs_hw_del(local);
  1105. destroy_workqueue(local->hw.workqueue);
  1106. fail_workqueue:
  1107. wiphy_unregister(local->hw.wiphy);
  1108. return result;
  1109. }
  1110. EXPORT_SYMBOL(ieee80211_register_hw);
  1111. int ieee80211_register_hwmode(struct ieee80211_hw *hw,
  1112. struct ieee80211_hw_mode *mode)
  1113. {
  1114. struct ieee80211_local *local = hw_to_local(hw);
  1115. struct ieee80211_rate *rate;
  1116. int i;
  1117. INIT_LIST_HEAD(&mode->list);
  1118. list_add_tail(&mode->list, &local->modes_list);
  1119. local->hw_modes |= (1 << mode->mode);
  1120. for (i = 0; i < mode->num_rates; i++) {
  1121. rate = &(mode->rates[i]);
  1122. rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
  1123. }
  1124. ieee80211_prepare_rates(local, mode);
  1125. if (!local->oper_hw_mode) {
  1126. /* Default to this mode */
  1127. local->hw.conf.phymode = mode->mode;
  1128. local->oper_hw_mode = local->scan_hw_mode = mode;
  1129. local->oper_channel = local->scan_channel = &mode->channels[0];
  1130. local->hw.conf.mode = local->oper_hw_mode;
  1131. local->hw.conf.chan = local->oper_channel;
  1132. }
  1133. if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
  1134. ieee80211_set_default_regdomain(mode);
  1135. return 0;
  1136. }
  1137. EXPORT_SYMBOL(ieee80211_register_hwmode);
  1138. void ieee80211_unregister_hw(struct ieee80211_hw *hw)
  1139. {
  1140. struct ieee80211_local *local = hw_to_local(hw);
  1141. struct ieee80211_sub_if_data *sdata, *tmp;
  1142. int i;
  1143. tasklet_kill(&local->tx_pending_tasklet);
  1144. tasklet_kill(&local->tasklet);
  1145. rtnl_lock();
  1146. BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
  1147. local->reg_state = IEEE80211_DEV_UNREGISTERED;
  1148. if (local->apdev)
  1149. ieee80211_if_del_mgmt(local);
  1150. /*
  1151. * At this point, interface list manipulations are fine
  1152. * because the driver cannot be handing us frames any
  1153. * more and the tasklet is killed.
  1154. */
  1155. /*
  1156. * First, we remove all non-master interfaces. Do this because they
  1157. * may have bss pointer dependency on the master, and when we free
  1158. * the master these would be freed as well, breaking our list
  1159. * iteration completely.
  1160. */
  1161. list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
  1162. if (sdata->dev == local->mdev)
  1163. continue;
  1164. list_del(&sdata->list);
  1165. __ieee80211_if_del(local, sdata);
  1166. }
  1167. /* then, finally, remove the master interface */
  1168. __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
  1169. rtnl_unlock();
  1170. ieee80211_rx_bss_list_deinit(local->mdev);
  1171. ieee80211_clear_tx_pending(local);
  1172. sta_info_stop(local);
  1173. rate_control_deinitialize(local);
  1174. debugfs_hw_del(local);
  1175. for (i = 0; i < NUM_IEEE80211_MODES; i++) {
  1176. kfree(local->supp_rates[i]);
  1177. kfree(local->basic_rates[i]);
  1178. }
  1179. if (skb_queue_len(&local->skb_queue)
  1180. || skb_queue_len(&local->skb_queue_unreliable))
  1181. printk(KERN_WARNING "%s: skb_queue not empty\n",
  1182. wiphy_name(local->hw.wiphy));
  1183. skb_queue_purge(&local->skb_queue);
  1184. skb_queue_purge(&local->skb_queue_unreliable);
  1185. destroy_workqueue(local->hw.workqueue);
  1186. wiphy_unregister(local->hw.wiphy);
  1187. ieee80211_wep_free(local);
  1188. ieee80211_led_exit(local);
  1189. }
  1190. EXPORT_SYMBOL(ieee80211_unregister_hw);
  1191. void ieee80211_free_hw(struct ieee80211_hw *hw)
  1192. {
  1193. struct ieee80211_local *local = hw_to_local(hw);
  1194. ieee80211_if_free(local->mdev);
  1195. wiphy_free(local->hw.wiphy);
  1196. }
  1197. EXPORT_SYMBOL(ieee80211_free_hw);
  1198. static int __init ieee80211_init(void)
  1199. {
  1200. struct sk_buff *skb;
  1201. int ret;
  1202. BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
  1203. ret = ieee80211_wme_register();
  1204. if (ret) {
  1205. printk(KERN_DEBUG "ieee80211_init: failed to "
  1206. "initialize WME (err=%d)\n", ret);
  1207. return ret;
  1208. }
  1209. ieee80211_debugfs_netdev_init();
  1210. ieee80211_regdomain_init();
  1211. return 0;
  1212. }
  1213. static void __exit ieee80211_exit(void)
  1214. {
  1215. ieee80211_wme_unregister();
  1216. ieee80211_debugfs_netdev_exit();
  1217. }
  1218. subsys_initcall(ieee80211_init);
  1219. module_exit(ieee80211_exit);
  1220. MODULE_DESCRIPTION("IEEE 802.11 subsystem");
  1221. MODULE_LICENSE("GPL");