blob: 34b11eae30c80340bf8de2cb2363f2c951af5c93 [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);
36 cfg80211_put_bss(wdev->current_bss);
37 }
38
39 cfg80211_hold_bss(bss);
40 wdev->current_bss = 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);
81 cfg80211_put_bss(wdev->current_bss);
82 }
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{
95 int err;
96
97 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
98
99 if (err)
100 return err;
101
Johannes Berg9d308422009-04-20 18:43:46 +0200102 cfg80211_clear_ibss(dev, nowext);
Johannes Berg04a773a2009-04-19 21:24:32 +0200103
104 return 0;
105}
106
107#ifdef CONFIG_WIRELESS_EXT
108static int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
109 struct wireless_dev *wdev)
110{
111 enum ieee80211_band band;
112 int i;
113
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200114 if (!wdev->wext.ibss.beacon_interval)
115 wdev->wext.ibss.beacon_interval = 100;
Johannes Berg8e30bc52009-04-22 17:45:38 +0200116
Johannes Berg04a773a2009-04-19 21:24:32 +0200117 /* try to find an IBSS channel if none requested ... */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200118 if (!wdev->wext.ibss.channel) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200119 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
120 struct ieee80211_supported_band *sband;
121 struct ieee80211_channel *chan;
122
123 sband = rdev->wiphy.bands[band];
124 if (!sband)
125 continue;
126
127 for (i = 0; i < sband->n_channels; i++) {
128 chan = &sband->channels[i];
129 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
130 continue;
131 if (chan->flags & IEEE80211_CHAN_DISABLED)
132 continue;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200133 wdev->wext.ibss.channel = chan;
Johannes Berg04a773a2009-04-19 21:24:32 +0200134 break;
135 }
136
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200137 if (wdev->wext.ibss.channel)
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 return -EINVAL;
143 }
144
145 /* don't join -- SSID is not there */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200146 if (!wdev->wext.ibss.ssid_len)
Johannes Berg04a773a2009-04-19 21:24:32 +0200147 return 0;
148
149 if (!netif_running(wdev->netdev))
150 return 0;
151
152 return cfg80211_join_ibss(wiphy_to_dev(wdev->wiphy),
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200153 wdev->netdev, &wdev->wext.ibss);
Johannes Berg04a773a2009-04-19 21:24:32 +0200154}
155
156int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
157 struct iw_request_info *info,
158 struct iw_freq *freq, char *extra)
159{
160 struct wireless_dev *wdev = dev->ieee80211_ptr;
161 struct ieee80211_channel *chan;
162 int err;
163
164 /* call only for ibss! */
165 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
166 return -EINVAL;
167
168 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
169 return -EOPNOTSUPP;
170
171 chan = cfg80211_wext_freq(wdev->wiphy, freq);
172 if (chan && IS_ERR(chan))
173 return PTR_ERR(chan);
174
175 if (chan &&
176 (chan->flags & IEEE80211_CHAN_NO_IBSS ||
177 chan->flags & IEEE80211_CHAN_DISABLED))
178 return -EINVAL;
179
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200180 if (wdev->wext.ibss.channel == chan)
Johannes Berg04a773a2009-04-19 21:24:32 +0200181 return 0;
182
183 if (wdev->ssid_len) {
Johannes Berg9d308422009-04-20 18:43:46 +0200184 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
185 dev, true);
Johannes Berg04a773a2009-04-19 21:24:32 +0200186 if (err)
187 return err;
188 }
189
190 if (chan) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200191 wdev->wext.ibss.channel = chan;
192 wdev->wext.ibss.channel_fixed = true;
Johannes Berg04a773a2009-04-19 21:24:32 +0200193 } else {
194 /* cfg80211_ibss_wext_join will pick one if needed */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200195 wdev->wext.ibss.channel_fixed = false;
Johannes Berg04a773a2009-04-19 21:24:32 +0200196 }
197
198 return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
199}
200/* temporary symbol - mark GPL - in the future the handler won't be */
201EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwfreq);
202
203int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
204 struct iw_request_info *info,
205 struct iw_freq *freq, char *extra)
206{
207 struct wireless_dev *wdev = dev->ieee80211_ptr;
208 struct ieee80211_channel *chan = NULL;
209
210 /* call only for ibss! */
211 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
212 return -EINVAL;
213
214 if (wdev->current_bss)
215 chan = wdev->current_bss->channel;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200216 else if (wdev->wext.ibss.channel)
217 chan = wdev->wext.ibss.channel;
Johannes Berg04a773a2009-04-19 21:24:32 +0200218
219 if (chan) {
220 freq->m = chan->center_freq;
221 freq->e = 6;
222 return 0;
223 }
224
225 /* no channel if not joining */
226 return -EINVAL;
227}
228/* temporary symbol - mark GPL - in the future the handler won't be */
229EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwfreq);
230
231int cfg80211_ibss_wext_siwessid(struct net_device *dev,
232 struct iw_request_info *info,
233 struct iw_point *data, char *ssid)
234{
235 struct wireless_dev *wdev = dev->ieee80211_ptr;
236 size_t len = data->length;
237 int err;
238
239 /* call only for ibss! */
240 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
241 return -EINVAL;
242
243 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
244 return -EOPNOTSUPP;
245
246 if (wdev->ssid_len) {
Johannes Berg9d308422009-04-20 18:43:46 +0200247 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
248 dev, true);
Johannes Berg04a773a2009-04-19 21:24:32 +0200249 if (err)
250 return err;
251 }
252
253 /* iwconfig uses nul termination in SSID.. */
254 if (len > 0 && ssid[len - 1] == '\0')
255 len--;
256
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200257 wdev->wext.ibss.ssid = wdev->ssid;
258 memcpy(wdev->wext.ibss.ssid, ssid, len);
259 wdev->wext.ibss.ssid_len = len;
Johannes Berg04a773a2009-04-19 21:24:32 +0200260
261 return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
262}
263/* temporary symbol - mark GPL - in the future the handler won't be */
264EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwessid);
265
266int cfg80211_ibss_wext_giwessid(struct net_device *dev,
267 struct iw_request_info *info,
268 struct iw_point *data, char *ssid)
269{
270 struct wireless_dev *wdev = dev->ieee80211_ptr;
271
272 /* call only for ibss! */
273 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
274 return -EINVAL;
275
276 data->flags = 0;
277
278 if (wdev->ssid_len) {
279 data->flags = 1;
280 data->length = wdev->ssid_len;
281 memcpy(ssid, wdev->ssid, data->length);
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200282 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
Johannes Berg04a773a2009-04-19 21:24:32 +0200283 data->flags = 1;
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200284 data->length = wdev->wext.ibss.ssid_len;
285 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
Johannes Berg04a773a2009-04-19 21:24:32 +0200286 }
287
288 return 0;
289}
290/* temporary symbol - mark GPL - in the future the handler won't be */
291EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwessid);
292
293int cfg80211_ibss_wext_siwap(struct net_device *dev,
294 struct iw_request_info *info,
295 struct sockaddr *ap_addr, char *extra)
296{
297 struct wireless_dev *wdev = dev->ieee80211_ptr;
298 u8 *bssid = ap_addr->sa_data;
299 int err;
300
301 /* call only for ibss! */
302 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
303 return -EINVAL;
304
305 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
306 return -EOPNOTSUPP;
307
308 if (ap_addr->sa_family != ARPHRD_ETHER)
309 return -EINVAL;
310
311 /* automatic mode */
312 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
313 bssid = NULL;
314
315 /* both automatic */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200316 if (!bssid && !wdev->wext.ibss.bssid)
Johannes Berg04a773a2009-04-19 21:24:32 +0200317 return 0;
318
319 /* fixed already - and no change */
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200320 if (wdev->wext.ibss.bssid && bssid &&
321 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
Johannes Berg04a773a2009-04-19 21:24:32 +0200322 return 0;
323
324 if (wdev->ssid_len) {
Johannes Berg9d308422009-04-20 18:43:46 +0200325 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
326 dev, true);
Johannes Berg04a773a2009-04-19 21:24:32 +0200327 if (err)
328 return err;
329 }
330
331 if (bssid) {
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200332 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
333 wdev->wext.ibss.bssid = wdev->wext.bssid;
Johannes Berg04a773a2009-04-19 21:24:32 +0200334 } else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200335 wdev->wext.ibss.bssid = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +0200336
337 return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
338}
339/* temporary symbol - mark GPL - in the future the handler won't be */
340EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwap);
341
342int cfg80211_ibss_wext_giwap(struct net_device *dev,
343 struct iw_request_info *info,
344 struct sockaddr *ap_addr, char *extra)
345{
346 struct wireless_dev *wdev = dev->ieee80211_ptr;
347
348 /* call only for ibss! */
349 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
350 return -EINVAL;
351
352 ap_addr->sa_family = ARPHRD_ETHER;
353
Johannes Berg7ebbe6b2009-07-01 21:26:48 +0200354 if (wdev->current_bss)
355 memcpy(ap_addr->sa_data, wdev->current_bss->bssid, ETH_ALEN);
356 else
Johannes Bergcbe8fa92009-05-09 20:09:03 +0200357 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
Johannes Berg04a773a2009-04-19 21:24:32 +0200358 return 0;
359}
360/* temporary symbol - mark GPL - in the future the handler won't be */
361EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwap);
362#endif