blob: a5330c5a54777c25d026ee9f3e84de89c3425824 [file] [log] [blame]
Johannes Berg04a773a2009-04-19 21:24:32 +02001/*
2 * Some IBSS support code for cfg80211.
3 *
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
5 */
6
7#include <linux/etherdevice.h>
8#include <linux/if_arp.h>
9#include <net/cfg80211.h>
Johannes Berg04a773a2009-04-19 21:24:32 +020010#include "nl80211.h"
11
12
13void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
14{
15 struct wireless_dev *wdev = dev->ieee80211_ptr;
16 struct cfg80211_bss *bss;
17#ifdef CONFIG_WIRELESS_EXT
18 union iwreq_data wrqu;
19#endif
20
21 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
22 return;
23
24 if (WARN_ON(!wdev->ssid_len))
25 return;
26
Johannes Berg04a773a2009-04-19 21:24:32 +020027 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
28 wdev->ssid, wdev->ssid_len,
29 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
30
31 if (WARN_ON(!bss))
32 return;
33
34 if (wdev->current_bss) {
35 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +020036 cfg80211_put_bss(&wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +020037 }
38
Johannes Berg19957bb2009-07-02 17:20:43 +020039 cfg80211_hold_bss(bss_from_pub(bss));
40 wdev->current_bss = bss_from_pub(bss);
Johannes Berg04a773a2009-04-19 21:24:32 +020041
42 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid, gfp);
43#ifdef CONFIG_WIRELESS_EXT
44 memset(&wrqu, 0, sizeof(wrqu));
45 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
46 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
47#endif
48}
49EXPORT_SYMBOL(cfg80211_ibss_joined);
50
51int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
52 struct net_device *dev,
53 struct cfg80211_ibss_params *params)
54{
55 struct wireless_dev *wdev = dev->ieee80211_ptr;
56 int err;
57
58 if (wdev->ssid_len)
59 return -EALREADY;
60
61#ifdef CONFIG_WIRELESS_EXT
Johannes Bergcbe8fa92009-05-09 20:09:03 +020062 wdev->wext.ibss.channel = params->channel;
Johannes Berg04a773a2009-04-19 21:24:32 +020063#endif
64 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
65
66 if (err)
67 return err;
68
69 memcpy(wdev->ssid, params->ssid, params->ssid_len);
70 wdev->ssid_len = params->ssid_len;
71
72 return 0;
73}
74
Johannes Berg9d308422009-04-20 18:43:46 +020075void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
Johannes Berg04a773a2009-04-19 21:24:32 +020076{
77 struct wireless_dev *wdev = dev->ieee80211_ptr;
78
79 if (wdev->current_bss) {
80 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg19957bb2009-07-02 17:20:43 +020081 cfg80211_put_bss(&wdev->current_bss->pub);
Johannes Berg04a773a2009-04-19 21:24:32 +020082 }
83
84 wdev->current_bss = NULL;
85 wdev->ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +020086#ifdef CONFIG_WIRELESS_EXT
87 if (!nowext)
Johannes Bergcbe8fa92009-05-09 20:09:03 +020088 wdev->wext.ibss.ssid_len = 0;
Johannes Berg9d308422009-04-20 18:43:46 +020089#endif
Johannes Berg04a773a2009-04-19 21:24:32 +020090}
91
92int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
Johannes Berg9d308422009-04-20 18:43:46 +020093 struct net_device *dev, bool nowext)
Johannes Berg04a773a2009-04-19 21:24:32 +020094{
Johannes Berg78485472009-07-07 03:56:05 +020095 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg04a773a2009-04-19 21:24:32 +020096 int err;
97
Johannes Berg78485472009-07-07 03:56:05 +020098 if (!wdev->ssid_len)
99 return -ENOLINK;
100
Johannes Berg04a773a2009-04-19 21:24:32 +0200101 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
102
103 if (err)
104 return err;
105
Johannes Berg9d308422009-04-20 18:43:46 +0200106 cfg80211_clear_ibss(dev, nowext);
Johannes Berg04a773a2009-04-19 21:24:32 +0200107
108 return 0;
109}
110
111#ifdef CONFIG_WIRELESS_EXT
112static int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
113 struct wireless_dev *wdev)
114{
115 enum ieee80211_band band;
116 int i;
117
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200118 if (!wdev->wext.ibss.beacon_interval)
119 wdev->wext.ibss.beacon_interval = 100;
Johannes Berg8e30bc52009-04-22 17:45:38 +0200120
Johannes Berg04a773a2009-04-19 21:24:32 +0200121 /* try to find an IBSS channel if none requested ... */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200122 if (!wdev->wext.ibss.channel) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200123 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
124 struct ieee80211_supported_band *sband;
125 struct ieee80211_channel *chan;
126
127 sband = rdev->wiphy.bands[band];
128 if (!sband)
129 continue;
130
131 for (i = 0; i < sband->n_channels; i++) {
132 chan = &sband->channels[i];
133 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
134 continue;
135 if (chan->flags & IEEE80211_CHAN_DISABLED)
136 continue;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200137 wdev->wext.ibss.channel = chan;
Johannes Berg04a773a2009-04-19 21:24:32 +0200138 break;
139 }
140
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200141 if (wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200142 break;
143 }
144
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200145 if (!wdev->wext.ibss.channel)
Johannes Berg04a773a2009-04-19 21:24:32 +0200146 return -EINVAL;
147 }
148
149 /* don't join -- SSID is not there */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200150 if (!wdev->wext.ibss.ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +0200151 return 0;
152
153 if (!netif_running(wdev->netdev))
154 return 0;
155
156 return cfg80211_join_ibss(wiphy_to_dev(wdev->wiphy),
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200157 wdev->netdev, &wdev->wext.ibss);
Johannes Berg04a773a2009-04-19 21:24:32 +0200158}
159
160int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
161 struct iw_request_info *info,
162 struct iw_freq *freq, char *extra)
163{
164 struct wireless_dev *wdev = dev->ieee80211_ptr;
165 struct ieee80211_channel *chan;
166 int err;
167
168 /* call only for ibss! */
169 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
170 return -EINVAL;
171
172 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
173 return -EOPNOTSUPP;
174
175 chan = cfg80211_wext_freq(wdev->wiphy, freq);
176 if (chan && IS_ERR(chan))
177 return PTR_ERR(chan);
178
179 if (chan &&
180 (chan->flags & IEEE80211_CHAN_NO_IBSS ||
181 chan->flags & IEEE80211_CHAN_DISABLED))
182 return -EINVAL;
183
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200184 if (wdev->wext.ibss.channel == chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200185 return 0;
186
187 if (wdev->ssid_len) {
Johannes Berg9d308422009-04-20 18:43:46 +0200188 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
189 dev, true);
Johannes Berg04a773a2009-04-19 21:24:32 +0200190 if (err)
191 return err;
192 }
193
194 if (chan) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200195 wdev->wext.ibss.channel = chan;
196 wdev->wext.ibss.channel_fixed = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200197 } else {
198 /* cfg80211_ibss_wext_join will pick one if needed */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200199 wdev->wext.ibss.channel_fixed = false;
Johannes Berg04a773a2009-04-19 21:24:32 +0200200 }
201
202 return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
203}
204/* temporary symbol - mark GPL - in the future the handler won't be */
205EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwfreq);
206
207int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
208 struct iw_request_info *info,
209 struct iw_freq *freq, char *extra)
210{
211 struct wireless_dev *wdev = dev->ieee80211_ptr;
212 struct ieee80211_channel *chan = NULL;
213
214 /* call only for ibss! */
215 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
216 return -EINVAL;
217
218 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200219 chan = wdev->current_bss->pub.channel;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200220 else if (wdev->wext.ibss.channel)
221 chan = wdev->wext.ibss.channel;
Johannes Berg04a773a2009-04-19 21:24:32 +0200222
223 if (chan) {
224 freq->m = chan->center_freq;
225 freq->e = 6;
226 return 0;
227 }
228
229 /* no channel if not joining */
230 return -EINVAL;
231}
232/* temporary symbol - mark GPL - in the future the handler won't be */
233EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwfreq);
234
235int cfg80211_ibss_wext_siwessid(struct net_device *dev,
236 struct iw_request_info *info,
237 struct iw_point *data, char *ssid)
238{
239 struct wireless_dev *wdev = dev->ieee80211_ptr;
240 size_t len = data->length;
241 int err;
242
243 /* call only for ibss! */
244 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
245 return -EINVAL;
246
247 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
248 return -EOPNOTSUPP;
249
250 if (wdev->ssid_len) {
Johannes Berg9d308422009-04-20 18:43:46 +0200251 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
252 dev, true);
Johannes Berg04a773a2009-04-19 21:24:32 +0200253 if (err)
254 return err;
255 }
256
257 /* iwconfig uses nul termination in SSID.. */
258 if (len > 0 && ssid[len - 1] == '\0')
259 len--;
260
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200261 wdev->wext.ibss.ssid = wdev->ssid;
262 memcpy(wdev->wext.ibss.ssid, ssid, len);
263 wdev->wext.ibss.ssid_len = len;
Johannes Berg04a773a2009-04-19 21:24:32 +0200264
265 return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
266}
267/* temporary symbol - mark GPL - in the future the handler won't be */
268EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwessid);
269
270int cfg80211_ibss_wext_giwessid(struct net_device *dev,
271 struct iw_request_info *info,
272 struct iw_point *data, char *ssid)
273{
274 struct wireless_dev *wdev = dev->ieee80211_ptr;
275
276 /* call only for ibss! */
277 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
278 return -EINVAL;
279
280 data->flags = 0;
281
282 if (wdev->ssid_len) {
283 data->flags = 1;
284 data->length = wdev->ssid_len;
285 memcpy(ssid, wdev->ssid, data->length);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200286 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200287 data->flags = 1;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200288 data->length = wdev->wext.ibss.ssid_len;
289 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
Johannes Berg04a773a2009-04-19 21:24:32 +0200290 }
291
292 return 0;
293}
294/* temporary symbol - mark GPL - in the future the handler won't be */
295EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwessid);
296
297int cfg80211_ibss_wext_siwap(struct net_device *dev,
298 struct iw_request_info *info,
299 struct sockaddr *ap_addr, char *extra)
300{
301 struct wireless_dev *wdev = dev->ieee80211_ptr;
302 u8 *bssid = ap_addr->sa_data;
303 int err;
304
305 /* call only for ibss! */
306 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
307 return -EINVAL;
308
309 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
310 return -EOPNOTSUPP;
311
312 if (ap_addr->sa_family != ARPHRD_ETHER)
313 return -EINVAL;
314
315 /* automatic mode */
316 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
317 bssid = NULL;
318
319 /* both automatic */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200320 if (!bssid && !wdev->wext.ibss.bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +0200321 return 0;
322
323 /* fixed already - and no change */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200324 if (wdev->wext.ibss.bssid && bssid &&
325 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
Johannes Berg04a773a2009-04-19 21:24:32 +0200326 return 0;
327
328 if (wdev->ssid_len) {
Johannes Berg9d308422009-04-20 18:43:46 +0200329 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
330 dev, true);
Johannes Berg04a773a2009-04-19 21:24:32 +0200331 if (err)
332 return err;
333 }
334
335 if (bssid) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200336 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
337 wdev->wext.ibss.bssid = wdev->wext.bssid;
Johannes Berg04a773a2009-04-19 21:24:32 +0200338 } else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200339 wdev->wext.ibss.bssid = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200340
341 return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
342}
343/* temporary symbol - mark GPL - in the future the handler won't be */
344EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwap);
345
346int cfg80211_ibss_wext_giwap(struct net_device *dev,
347 struct iw_request_info *info,
348 struct sockaddr *ap_addr, char *extra)
349{
350 struct wireless_dev *wdev = dev->ieee80211_ptr;
351
352 /* call only for ibss! */
353 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
354 return -EINVAL;
355
356 ap_addr->sa_family = ARPHRD_ETHER;
357
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200358 if (wdev->current_bss)
Johannes Berg19957bb2009-07-02 17:20:43 +0200359 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200360 else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200361 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
Johannes Berg04a773a2009-04-19 21:24:32 +0200362 return 0;
363}
364/* temporary symbol - mark GPL - in the future the handler won't be */
365EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwap);
366#endif