cfg80211: fix MFP bug, sparse warnings
[deliverable/linux.git] / net / wireless / ibss.c
CommitLineData
04a773ad
JB
1/*
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>
04a773ad
JB
10#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
04a773ad
JB
27 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);
19957bb3 36 cfg80211_put_bss(&wdev->current_bss->pub);
04a773ad
JB
37 }
38
19957bb3
JB
39 cfg80211_hold_bss(bss_from_pub(bss));
40 wdev->current_bss = bss_from_pub(bss);
04a773ad
JB
41
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
cbe8fa9c 62 wdev->wext.ibss.channel = params->channel;
04a773ad
JB
63#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
9d308429 75void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
04a773ad
JB
76{
77 struct wireless_dev *wdev = dev->ieee80211_ptr;
78
79 if (wdev->current_bss) {
80 cfg80211_unhold_bss(wdev->current_bss);
19957bb3 81 cfg80211_put_bss(&wdev->current_bss->pub);
04a773ad
JB
82 }
83
84 wdev->current_bss = NULL;
85 wdev->ssid_len = 0;
9d308429
JB
86#ifdef CONFIG_WIRELESS_EXT
87 if (!nowext)
cbe8fa9c 88 wdev->wext.ibss.ssid_len = 0;
9d308429 89#endif
04a773ad
JB
90}
91
92int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
9d308429 93 struct net_device *dev, bool nowext)
04a773ad 94{
78485475 95 struct wireless_dev *wdev = dev->ieee80211_ptr;
04a773ad
JB
96 int err;
97
78485475
JB
98 if (!wdev->ssid_len)
99 return -ENOLINK;
100
04a773ad
JB
101 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
102
103 if (err)
104 return err;
105
9d308429 106 cfg80211_clear_ibss(dev, nowext);
04a773ad
JB
107
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
cbe8fa9c
JB
118 if (!wdev->wext.ibss.beacon_interval)
119 wdev->wext.ibss.beacon_interval = 100;
8e30bc55 120
04a773ad 121 /* try to find an IBSS channel if none requested ... */
cbe8fa9c 122 if (!wdev->wext.ibss.channel) {
04a773ad
JB
123 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;
cbe8fa9c 137 wdev->wext.ibss.channel = chan;
04a773ad
JB
138 break;
139 }
140
cbe8fa9c 141 if (wdev->wext.ibss.channel)
04a773ad
JB
142 break;
143 }
144
cbe8fa9c 145 if (!wdev->wext.ibss.channel)
04a773ad
JB
146 return -EINVAL;
147 }
148
149 /* don't join -- SSID is not there */
cbe8fa9c 150 if (!wdev->wext.ibss.ssid_len)
04a773ad
JB
151 return 0;
152
153 if (!netif_running(wdev->netdev))
154 return 0;
155
156 return cfg80211_join_ibss(wiphy_to_dev(wdev->wiphy),
cbe8fa9c 157 wdev->netdev, &wdev->wext.ibss);
04a773ad
JB
158}
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
cbe8fa9c 184 if (wdev->wext.ibss.channel == chan)
04a773ad
JB
185 return 0;
186
187 if (wdev->ssid_len) {
9d308429
JB
188 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
189 dev, true);
04a773ad
JB
190 if (err)
191 return err;
192 }
193
194 if (chan) {
cbe8fa9c
JB
195 wdev->wext.ibss.channel = chan;
196 wdev->wext.ibss.channel_fixed = true;
04a773ad
JB
197 } else {
198 /* cfg80211_ibss_wext_join will pick one if needed */
cbe8fa9c 199 wdev->wext.ibss.channel_fixed = false;
04a773ad
JB
200 }
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)
19957bb3 219 chan = wdev->current_bss->pub.channel;
cbe8fa9c
JB
220 else if (wdev->wext.ibss.channel)
221 chan = wdev->wext.ibss.channel;
04a773ad
JB
222
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) {
9d308429
JB
251 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
252 dev, true);
04a773ad
JB
253 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
cbe8fa9c
JB
261 wdev->wext.ibss.ssid = wdev->ssid;
262 memcpy(wdev->wext.ibss.ssid, ssid, len);
263 wdev->wext.ibss.ssid_len = len;
04a773ad
JB
264
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);
cbe8fa9c 286 } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
04a773ad 287 data->flags = 1;
cbe8fa9c
JB
288 data->length = wdev->wext.ibss.ssid_len;
289 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
04a773ad
JB
290 }
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 */
cbe8fa9c 320 if (!bssid && !wdev->wext.ibss.bssid)
04a773ad
JB
321 return 0;
322
323 /* fixed already - and no change */
cbe8fa9c
JB
324 if (wdev->wext.ibss.bssid && bssid &&
325 compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
04a773ad
JB
326 return 0;
327
328 if (wdev->ssid_len) {
9d308429
JB
329 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
330 dev, true);
04a773ad
JB
331 if (err)
332 return err;
333 }
334
335 if (bssid) {
cbe8fa9c
JB
336 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
337 wdev->wext.ibss.bssid = wdev->wext.bssid;
04a773ad 338 } else
cbe8fa9c 339 wdev->wext.ibss.bssid = NULL;
04a773ad
JB
340
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
7ebbe6bd 358 if (wdev->current_bss)
19957bb3 359 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
7ebbe6bd 360 else
cbe8fa9c 361 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
04a773ad
JB
362 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
This page took 0.104241 seconds and 5 git commands to generate.