cfg80211: fix netdev down problem
[safe/jmp/linux-2.6] / net / wireless / ibss.c
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>
10 #include "nl80211.h"
11
12
13 void 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
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);
36                 cfg80211_put_bss(&wdev->current_bss->pub);
37         }
38
39         cfg80211_hold_bss(bss_from_pub(bss));
40         wdev->current_bss = bss_from_pub(bss);
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 }
49 EXPORT_SYMBOL(cfg80211_ibss_joined);
50
51 int 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
62         wdev->wext.ibss.channel = params->channel;
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
75 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
76 {
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->pub);
82         }
83
84         wdev->current_bss = NULL;
85         wdev->ssid_len = 0;
86 #ifdef CONFIG_WIRELESS_EXT
87         if (!nowext)
88                 wdev->wext.ibss.ssid_len = 0;
89 #endif
90 }
91
92 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
93                         struct net_device *dev, bool nowext)
94 {
95         struct wireless_dev *wdev = dev->ieee80211_ptr;
96         int err;
97
98         if (!wdev->ssid_len)
99                 return -ENOLINK;
100
101         err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
102
103         if (err)
104                 return err;
105
106         cfg80211_clear_ibss(dev, nowext);
107
108         return 0;
109 }
110
111 #ifdef CONFIG_WIRELESS_EXT
112 static 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
118         if (!wdev->wext.ibss.beacon_interval)
119                 wdev->wext.ibss.beacon_interval = 100;
120
121         /* try to find an IBSS channel if none requested ... */
122         if (!wdev->wext.ibss.channel) {
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;
137                                 wdev->wext.ibss.channel = chan;
138                                 break;
139                         }
140
141                         if (wdev->wext.ibss.channel)
142                                 break;
143                 }
144
145                 if (!wdev->wext.ibss.channel)
146                         return -EINVAL;
147         }
148
149         /* don't join -- SSID is not there */
150         if (!wdev->wext.ibss.ssid_len)
151                 return 0;
152
153         if (!netif_running(wdev->netdev))
154                 return 0;
155
156         return cfg80211_join_ibss(wiphy_to_dev(wdev->wiphy),
157                                   wdev->netdev, &wdev->wext.ibss);
158 }
159
160 int 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
184         if (wdev->wext.ibss.channel == chan)
185                 return 0;
186
187         if (wdev->ssid_len) {
188                 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
189                                           dev, true);
190                 if (err)
191                         return err;
192         }
193
194         if (chan) {
195                 wdev->wext.ibss.channel = chan;
196                 wdev->wext.ibss.channel_fixed = true;
197         } else {
198                 /* cfg80211_ibss_wext_join will pick one if needed */
199                 wdev->wext.ibss.channel_fixed = false;
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 */
205 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwfreq);
206
207 int 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)
219                 chan = wdev->current_bss->pub.channel;
220         else if (wdev->wext.ibss.channel)
221                 chan = wdev->wext.ibss.channel;
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 */
233 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwfreq);
234
235 int 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) {
251                 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
252                                           dev, true);
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
261         wdev->wext.ibss.ssid = wdev->ssid;
262         memcpy(wdev->wext.ibss.ssid, ssid, len);
263         wdev->wext.ibss.ssid_len = len;
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 */
268 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwessid);
269
270 int 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);
286         } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
287                 data->flags = 1;
288                 data->length = wdev->wext.ibss.ssid_len;
289                 memcpy(ssid, wdev->wext.ibss.ssid, data->length);
290         }
291
292         return 0;
293 }
294 /* temporary symbol - mark GPL - in the future the handler won't be */
295 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwessid);
296
297 int 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 */
320         if (!bssid && !wdev->wext.ibss.bssid)
321                 return 0;
322
323         /* fixed already - and no change */
324         if (wdev->wext.ibss.bssid && bssid &&
325             compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0)
326                 return 0;
327
328         if (wdev->ssid_len) {
329                 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy),
330                                           dev, true);
331                 if (err)
332                         return err;
333         }
334
335         if (bssid) {
336                 memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
337                 wdev->wext.ibss.bssid = wdev->wext.bssid;
338         } else
339                 wdev->wext.ibss.bssid = NULL;
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 */
344 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwap);
345
346 int 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
358         if (wdev->current_bss)
359                 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
360         else
361                 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
362         return 0;
363 }
364 /* temporary symbol - mark GPL - in the future the handler won't be */
365 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwap);
366 #endif