ibss.c 12 KB

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