Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
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); | |
36 | cfg80211_put_bss(wdev->current_bss); | |
37 | } | |
38 | ||
39 | cfg80211_hold_bss(bss); | |
40 | wdev->current_bss = 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 | } | |
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 | |
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 | 75 | void 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); | |
81 | cfg80211_put_bss(wdev->current_bss); | |
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 | ||
92 | int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev, | |
9d308429 | 93 | struct net_device *dev, bool nowext) |
04a773ad JB |
94 | { |
95 | int err; | |
96 | ||
97 | err = rdev->ops->leave_ibss(&rdev->wiphy, dev); | |
98 | ||
99 | if (err) | |
100 | return err; | |
101 | ||
9d308429 | 102 | cfg80211_clear_ibss(dev, nowext); |
04a773ad JB |
103 | |
104 | return 0; | |
105 | } | |
106 | ||
107 | #ifdef CONFIG_WIRELESS_EXT | |
108 | static 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 | ||
cbe8fa9c JB |
114 | if (!wdev->wext.ibss.beacon_interval) |
115 | wdev->wext.ibss.beacon_interval = 100; | |
8e30bc55 | 116 | |
04a773ad | 117 | /* try to find an IBSS channel if none requested ... */ |
cbe8fa9c | 118 | if (!wdev->wext.ibss.channel) { |
04a773ad JB |
119 | 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; | |
cbe8fa9c | 133 | wdev->wext.ibss.channel = chan; |
04a773ad JB |
134 | break; |
135 | } | |
136 | ||
cbe8fa9c | 137 | if (wdev->wext.ibss.channel) |
04a773ad JB |
138 | break; |
139 | } | |
140 | ||
cbe8fa9c | 141 | if (!wdev->wext.ibss.channel) |
04a773ad JB |
142 | return -EINVAL; |
143 | } | |
144 | ||
145 | /* don't join -- SSID is not there */ | |
cbe8fa9c | 146 | if (!wdev->wext.ibss.ssid_len) |
04a773ad JB |
147 | return 0; |
148 | ||
149 | if (!netif_running(wdev->netdev)) | |
150 | return 0; | |
151 | ||
152 | return cfg80211_join_ibss(wiphy_to_dev(wdev->wiphy), | |
cbe8fa9c | 153 | wdev->netdev, &wdev->wext.ibss); |
04a773ad JB |
154 | } |
155 | ||
156 | int 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 | ||
cbe8fa9c | 180 | if (wdev->wext.ibss.channel == chan) |
04a773ad JB |
181 | return 0; |
182 | ||
183 | if (wdev->ssid_len) { | |
9d308429 JB |
184 | err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy), |
185 | dev, true); | |
04a773ad JB |
186 | if (err) |
187 | return err; | |
188 | } | |
189 | ||
190 | if (chan) { | |
cbe8fa9c JB |
191 | wdev->wext.ibss.channel = chan; |
192 | wdev->wext.ibss.channel_fixed = true; | |
04a773ad JB |
193 | } else { |
194 | /* cfg80211_ibss_wext_join will pick one if needed */ | |
cbe8fa9c | 195 | wdev->wext.ibss.channel_fixed = false; |
04a773ad JB |
196 | } |
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 */ | |
201 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwfreq); | |
202 | ||
203 | int 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; | |
cbe8fa9c JB |
216 | else if (wdev->wext.ibss.channel) |
217 | chan = wdev->wext.ibss.channel; | |
04a773ad JB |
218 | |
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 */ | |
229 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwfreq); | |
230 | ||
231 | int 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) { | |
9d308429 JB |
247 | err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy), |
248 | dev, true); | |
04a773ad JB |
249 | 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 | ||
cbe8fa9c JB |
257 | wdev->wext.ibss.ssid = wdev->ssid; |
258 | memcpy(wdev->wext.ibss.ssid, ssid, len); | |
259 | wdev->wext.ibss.ssid_len = len; | |
04a773ad JB |
260 | |
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 */ | |
264 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwessid); | |
265 | ||
266 | int 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); | |
cbe8fa9c | 282 | } else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) { |
04a773ad | 283 | data->flags = 1; |
cbe8fa9c JB |
284 | data->length = wdev->wext.ibss.ssid_len; |
285 | memcpy(ssid, wdev->wext.ibss.ssid, data->length); | |
04a773ad JB |
286 | } |
287 | ||
288 | return 0; | |
289 | } | |
290 | /* temporary symbol - mark GPL - in the future the handler won't be */ | |
291 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwessid); | |
292 | ||
293 | int 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 */ | |
cbe8fa9c | 316 | if (!bssid && !wdev->wext.ibss.bssid) |
04a773ad JB |
317 | return 0; |
318 | ||
319 | /* fixed already - and no change */ | |
cbe8fa9c JB |
320 | if (wdev->wext.ibss.bssid && bssid && |
321 | compare_ether_addr(bssid, wdev->wext.ibss.bssid) == 0) | |
04a773ad JB |
322 | return 0; |
323 | ||
324 | if (wdev->ssid_len) { | |
9d308429 JB |
325 | err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy), |
326 | dev, true); | |
04a773ad JB |
327 | if (err) |
328 | return err; | |
329 | } | |
330 | ||
331 | if (bssid) { | |
cbe8fa9c JB |
332 | memcpy(wdev->wext.bssid, bssid, ETH_ALEN); |
333 | wdev->wext.ibss.bssid = wdev->wext.bssid; | |
04a773ad | 334 | } else |
cbe8fa9c | 335 | wdev->wext.ibss.bssid = NULL; |
04a773ad JB |
336 | |
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 */ | |
340 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwap); | |
341 | ||
342 | int 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 | ||
7ebbe6bd JB |
354 | if (wdev->current_bss) |
355 | memcpy(ap_addr->sa_data, wdev->current_bss->bssid, ETH_ALEN); | |
356 | else | |
cbe8fa9c | 357 | memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN); |
04a773ad JB |
358 | return 0; |
359 | } | |
360 | /* temporary symbol - mark GPL - in the future the handler won't be */ | |
361 | EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwap); | |
362 | #endif |