ibss.c 12 KB

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