ibss.c 11 KB

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