ibss.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Some IBSS support code for cfg80211.
  3. *
  4. * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
  5. */
  6. #include <linux/etherdevice.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/slab.h>
  9. #include <linux/export.h>
  10. #include <net/cfg80211.h>
  11. #include "wext-compat.h"
  12. #include "nl80211.h"
  13. void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid)
  14. {
  15. struct wireless_dev *wdev = dev->ieee80211_ptr;
  16. struct cfg80211_bss *bss;
  17. #ifdef CONFIG_CFG80211_WEXT
  18. union iwreq_data wrqu;
  19. #endif
  20. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  21. return;
  22. if (!wdev->ssid_len)
  23. return;
  24. bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
  25. wdev->ssid, wdev->ssid_len,
  26. WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
  27. if (WARN_ON(!bss))
  28. return;
  29. if (wdev->current_bss) {
  30. cfg80211_unhold_bss(wdev->current_bss);
  31. cfg80211_put_bss(&wdev->current_bss->pub);
  32. }
  33. cfg80211_hold_bss(bss_from_pub(bss));
  34. wdev->current_bss = bss_from_pub(bss);
  35. cfg80211_upload_connect_keys(wdev);
  36. nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid,
  37. GFP_KERNEL);
  38. #ifdef CONFIG_CFG80211_WEXT
  39. memset(&wrqu, 0, sizeof(wrqu));
  40. memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
  41. wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
  42. #endif
  43. }
  44. void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
  45. {
  46. struct wireless_dev *wdev = dev->ieee80211_ptr;
  47. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  48. struct cfg80211_event *ev;
  49. unsigned long flags;
  50. CFG80211_DEV_WARN_ON(!wdev->ssid_len);
  51. ev = kzalloc(sizeof(*ev), gfp);
  52. if (!ev)
  53. return;
  54. ev->type = EVENT_IBSS_JOINED;
  55. memcpy(ev->cr.bssid, bssid, ETH_ALEN);
  56. spin_lock_irqsave(&wdev->event_lock, flags);
  57. list_add_tail(&ev->list, &wdev->event_list);
  58. spin_unlock_irqrestore(&wdev->event_lock, flags);
  59. queue_work(cfg80211_wq, &rdev->event_work);
  60. }
  61. EXPORT_SYMBOL(cfg80211_ibss_joined);
  62. int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  63. struct net_device *dev,
  64. struct cfg80211_ibss_params *params,
  65. struct cfg80211_cached_keys *connkeys)
  66. {
  67. struct wireless_dev *wdev = dev->ieee80211_ptr;
  68. int err;
  69. ASSERT_WDEV_LOCK(wdev);
  70. if (wdev->ssid_len)
  71. return -EALREADY;
  72. if (!params->basic_rates) {
  73. /*
  74. * If no rates were explicitly configured,
  75. * use the mandatory rate set for 11b or
  76. * 11a for maximum compatibility.
  77. */
  78. struct ieee80211_supported_band *sband =
  79. rdev->wiphy.bands[params->channel->band];
  80. int j;
  81. u32 flag = params->channel->band == IEEE80211_BAND_5GHZ ?
  82. IEEE80211_RATE_MANDATORY_A :
  83. IEEE80211_RATE_MANDATORY_B;
  84. for (j = 0; j < sband->n_bitrates; j++) {
  85. if (sband->bitrates[j].flags & flag)
  86. params->basic_rates |= BIT(j);
  87. }
  88. }
  89. if (WARN_ON(wdev->connect_keys))
  90. kfree(wdev->connect_keys);
  91. wdev->connect_keys = connkeys;
  92. #ifdef CONFIG_CFG80211_WEXT
  93. wdev->wext.ibss.channel = params->channel;
  94. #endif
  95. err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
  96. if (err) {
  97. wdev->connect_keys = NULL;
  98. return err;
  99. }
  100. memcpy(wdev->ssid, params->ssid, params->ssid_len);
  101. wdev->ssid_len = params->ssid_len;
  102. return 0;
  103. }
  104. int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
  105. struct net_device *dev,
  106. struct cfg80211_ibss_params *params,
  107. struct cfg80211_cached_keys *connkeys)
  108. {
  109. struct wireless_dev *wdev = dev->ieee80211_ptr;
  110. int err;
  111. mutex_lock(&rdev->devlist_mtx);
  112. wdev_lock(wdev);
  113. err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
  114. wdev_unlock(wdev);
  115. mutex_unlock(&rdev->devlist_mtx);
  116. return err;
  117. }
  118. static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
  119. {
  120. struct wireless_dev *wdev = dev->ieee80211_ptr;
  121. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  122. int i;
  123. ASSERT_WDEV_LOCK(wdev);
  124. kfree(wdev->connect_keys);
  125. wdev->connect_keys = NULL;
  126. if (rdev->ops->set_qos_map) {
  127. rdev->ops->set_qos_map(&rdev->wiphy, dev, NULL);
  128. }
  129. /*
  130. * Delete all the keys ... pairwise keys can't really
  131. * exist any more anyway, but default keys might.
  132. */
  133. if (rdev->ops->del_key)
  134. for (i = 0; i < 6; i++)
  135. rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
  136. if (wdev->current_bss) {
  137. cfg80211_unhold_bss(wdev->current_bss);
  138. cfg80211_put_bss(&wdev->current_bss->pub);
  139. }
  140. wdev->current_bss = NULL;
  141. wdev->ssid_len = 0;
  142. #ifdef CONFIG_CFG80211_WEXT
  143. if (!nowext)
  144. wdev->wext.ibss.ssid_len = 0;
  145. #endif
  146. }
  147. void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
  148. {
  149. struct wireless_dev *wdev = dev->ieee80211_ptr;
  150. wdev_lock(wdev);
  151. __cfg80211_clear_ibss(dev, nowext);
  152. wdev_unlock(wdev);
  153. }
  154. int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  155. struct net_device *dev, bool nowext)
  156. {
  157. struct wireless_dev *wdev = dev->ieee80211_ptr;
  158. int err;
  159. ASSERT_WDEV_LOCK(wdev);
  160. if (!wdev->ssid_len)
  161. return -ENOLINK;
  162. err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
  163. if (err)
  164. return err;
  165. __cfg80211_clear_ibss(dev, nowext);
  166. return 0;
  167. }
  168. int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
  169. struct net_device *dev, bool nowext)
  170. {
  171. struct wireless_dev *wdev = dev->ieee80211_ptr;
  172. int err;
  173. wdev_lock(wdev);
  174. err = __cfg80211_leave_ibss(rdev, dev, nowext);
  175. wdev_unlock(wdev);
  176. return err;
  177. }
  178. #ifdef CONFIG_CFG80211_WEXT
  179. int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
  180. struct wireless_dev *wdev)
  181. {
  182. struct cfg80211_cached_keys *ck = NULL;
  183. enum ieee80211_band band;
  184. int i, err;
  185. ASSERT_WDEV_LOCK(wdev);
  186. if (!wdev->wext.ibss.beacon_interval)
  187. wdev->wext.ibss.beacon_interval = 100;
  188. /* try to find an IBSS channel if none requested ... */
  189. if (!wdev->wext.ibss.channel) {
  190. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  191. struct ieee80211_supported_band *sband;
  192. struct ieee80211_channel *chan;
  193. sband = rdev->wiphy.bands[band];
  194. if (!sband)
  195. continue;
  196. for (i = 0; i < sband->n_channels; i++) {
  197. chan = &sband->channels[i];
  198. if (chan->flags & IEEE80211_CHAN_NO_IBSS)
  199. continue;
  200. if (chan->flags & IEEE80211_CHAN_DISABLED)
  201. continue;
  202. wdev->wext.ibss.channel = chan;
  203. break;
  204. }
  205. if (wdev->wext.ibss.channel)
  206. break;
  207. }
  208. if (!wdev->wext.ibss.channel)
  209. return -EINVAL;
  210. }
  211. /* don't join -- SSID is not there */
  212. if (!wdev->wext.ibss.ssid_len)
  213. return 0;
  214. if (!netif_running(wdev->netdev))
  215. return 0;
  216. if (wdev->wext.keys) {
  217. wdev->wext.keys->def = wdev->wext.default_key;
  218. wdev->wext.keys->defmgmt = wdev->wext.default_mgmt_key;
  219. }
  220. wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
  221. if (wdev->wext.keys) {
  222. ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
  223. if (!ck)
  224. return -ENOMEM;
  225. for (i = 0; i < 6; i++)
  226. ck->params[i].key = ck->data[i];
  227. }
  228. err = __cfg80211_join_ibss(rdev, wdev->netdev,
  229. &wdev->wext.ibss, ck);
  230. if (err)
  231. kfree(ck);
  232. return err;
  233. }
  234. int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
  235. struct iw_request_info *info,
  236. struct iw_freq *wextfreq, char *extra)
  237. {
  238. struct wireless_dev *wdev = dev->ieee80211_ptr;
  239. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  240. struct ieee80211_channel *chan = NULL;
  241. int err, freq;
  242. /* call only for ibss! */
  243. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  244. return -EINVAL;
  245. if (!rdev->ops->join_ibss)
  246. return -EOPNOTSUPP;
  247. freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
  248. if (freq < 0)
  249. return freq;
  250. if (freq) {
  251. chan = ieee80211_get_channel(wdev->wiphy, freq);
  252. if (!chan)
  253. return -EINVAL;
  254. if (chan->flags & IEEE80211_CHAN_NO_IBSS ||
  255. chan->flags & IEEE80211_CHAN_DISABLED)
  256. return -EINVAL;
  257. }
  258. if (wdev->wext.ibss.channel == chan)
  259. return 0;
  260. wdev_lock(wdev);
  261. err = 0;
  262. if (wdev->ssid_len)
  263. err = __cfg80211_leave_ibss(rdev, dev, true);
  264. wdev_unlock(wdev);
  265. if (err)
  266. return err;
  267. if (chan) {
  268. wdev->wext.ibss.channel = chan;
  269. wdev->wext.ibss.channel_fixed = true;
  270. } else {
  271. /* cfg80211_ibss_wext_join will pick one if needed */
  272. wdev->wext.ibss.channel_fixed = false;
  273. }
  274. mutex_lock(&rdev->devlist_mtx);
  275. wdev_lock(wdev);
  276. err = cfg80211_ibss_wext_join(rdev, wdev);
  277. wdev_unlock(wdev);
  278. mutex_unlock(&rdev->devlist_mtx);
  279. return err;
  280. }
  281. int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
  282. struct iw_request_info *info,
  283. struct iw_freq *freq, char *extra)
  284. {
  285. struct wireless_dev *wdev = dev->ieee80211_ptr;
  286. struct ieee80211_channel *chan = NULL;
  287. /* call only for ibss! */
  288. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  289. return -EINVAL;
  290. wdev_lock(wdev);
  291. if (wdev->current_bss)
  292. chan = wdev->current_bss->pub.channel;
  293. else if (wdev->wext.ibss.channel)
  294. chan = wdev->wext.ibss.channel;
  295. wdev_unlock(wdev);
  296. if (chan) {
  297. freq->m = chan->center_freq;
  298. freq->e = 6;
  299. return 0;
  300. }
  301. /* no channel if not joining */
  302. return -EINVAL;
  303. }
  304. int cfg80211_ibss_wext_siwessid(struct net_device *dev,
  305. struct iw_request_info *info,
  306. struct iw_point *data, char *ssid)
  307. {
  308. struct wireless_dev *wdev = dev->ieee80211_ptr;
  309. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  310. size_t len = data->length;
  311. int err;
  312. /* call only for ibss! */
  313. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  314. return -EINVAL;
  315. if (!rdev->ops->join_ibss)
  316. return -EOPNOTSUPP;
  317. wdev_lock(wdev);
  318. err = 0;
  319. if (wdev->ssid_len)
  320. err = __cfg80211_leave_ibss(rdev, dev, true);
  321. wdev_unlock(wdev);
  322. if (err)
  323. return err;
  324. /* iwconfig uses nul termination in SSID.. */
  325. if (len > 0 && ssid[len - 1] == '\0')
  326. len--;
  327. wdev->wext.ibss.ssid = wdev->ssid;
  328. memcpy(wdev->wext.ibss.ssid, ssid, len);
  329. wdev->wext.ibss.ssid_len = len;
  330. mutex_lock(&rdev->devlist_mtx);
  331. wdev_lock(wdev);
  332. err = cfg80211_ibss_wext_join(rdev, wdev);
  333. wdev_unlock(wdev);
  334. mutex_unlock(&rdev->devlist_mtx);
  335. return err;
  336. }
  337. int cfg80211_ibss_wext_giwessid(struct net_device *dev,
  338. struct iw_request_info *info,
  339. struct iw_point *data, char *ssid)
  340. {
  341. struct wireless_dev *wdev = dev->ieee80211_ptr;
  342. /* call only for ibss! */
  343. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  344. return -EINVAL;
  345. data->flags = 0;
  346. wdev_lock(wdev);
  347. if (wdev->ssid_len) {
  348. data->flags = 1;
  349. data->length = wdev->ssid_len;
  350. memcpy(ssid, wdev->ssid, data->length);
  351. } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
  352. data->flags = 1;
  353. data->length = wdev->wext.ibss.ssid_len;
  354. memcpy(ssid, wdev->wext.ibss.ssid, data->length);
  355. }
  356. wdev_unlock(wdev);
  357. return 0;
  358. }
  359. int cfg80211_ibss_wext_siwap(struct net_device *dev,
  360. struct iw_request_info *info,
  361. struct sockaddr *ap_addr, char *extra)
  362. {
  363. struct wireless_dev *wdev = dev->ieee80211_ptr;
  364. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  365. u8 *bssid = ap_addr->sa_data;
  366. int err;
  367. /* call only for ibss! */
  368. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  369. return -EINVAL;
  370. if (!rdev->ops->join_ibss)
  371. return -EOPNOTSUPP;
  372. if (ap_addr->sa_family != ARPHRD_ETHER)
  373. return -EINVAL;
  374. /* automatic mode */
  375. if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
  376. bssid = NULL;
  377. /* both automatic */
  378. if (!bssid && !wdev->wext.ibss.bssid)
  379. return 0;
  380. /* fixed already - and no change */
  381. if (wdev->wext.ibss.bssid && bssid &&
  382. compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
  383. return 0;
  384. wdev_lock(wdev);
  385. err = 0;
  386. if (wdev->ssid_len)
  387. err = __cfg80211_leave_ibss(rdev, dev, true);
  388. wdev_unlock(wdev);
  389. if (err)
  390. return err;
  391. if (bssid) {
  392. memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
  393. wdev->wext.ibss.bssid = wdev->wext.bssid;
  394. } else
  395. wdev->wext.ibss.bssid = NULL;
  396. mutex_lock(&rdev->devlist_mtx);
  397. wdev_lock(wdev);
  398. err = cfg80211_ibss_wext_join(rdev, wdev);
  399. wdev_unlock(wdev);
  400. mutex_unlock(&rdev->devlist_mtx);
  401. return err;
  402. }
  403. int cfg80211_ibss_wext_giwap(struct net_device *dev,
  404. struct iw_request_info *info,
  405. struct sockaddr *ap_addr, char *extra)
  406. {
  407. struct wireless_dev *wdev = dev->ieee80211_ptr;
  408. /* call only for ibss! */
  409. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
  410. return -EINVAL;
  411. ap_addr->sa_family = ARPHRD_ETHER;
  412. wdev_lock(wdev);
  413. if (wdev->current_bss)
  414. memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
  415. else if (wdev->wext.ibss.bssid)
  416. memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
  417. else
  418. memset(ap_addr->sa_data, 0, ETH_ALEN);
  419. wdev_unlock(wdev);
  420. return 0;
  421. }
  422. #endif