2 * Some IBSS support code for cfg80211.
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
7 #include <linux/etherdevice.h>
8 #include <linux/if_arp.h>
9 #include <net/cfg80211.h>
10 #include <net/wireless.h>
14 void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, gfp_t gfp)
16 struct wireless_dev *wdev = dev->ieee80211_ptr;
17 struct cfg80211_bss *bss;
18 #ifdef CONFIG_WIRELESS_EXT
19 union iwreq_data wrqu;
22 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
25 if (WARN_ON(!wdev->ssid_len))
28 if (memcmp(bssid, wdev->bssid, ETH_ALEN) == 0)
31 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
32 wdev->ssid, wdev->ssid_len,
33 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS);
38 if (wdev->current_bss) {
39 cfg80211_unhold_bss(wdev->current_bss);
40 cfg80211_put_bss(wdev->current_bss);
43 cfg80211_hold_bss(bss);
44 wdev->current_bss = bss;
45 memcpy(wdev->bssid, bssid, ETH_ALEN);
47 nl80211_send_ibss_bssid(wiphy_to_dev(wdev->wiphy), dev, bssid, gfp);
48 #ifdef CONFIG_WIRELESS_EXT
49 memset(&wrqu, 0, sizeof(wrqu));
50 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
51 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
54 EXPORT_SYMBOL(cfg80211_ibss_joined);
56 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
57 struct net_device *dev,
58 struct cfg80211_ibss_params *params)
60 struct wireless_dev *wdev = dev->ieee80211_ptr;
66 #ifdef CONFIG_WIRELESS_EXT
67 wdev->wext.channel = params->channel;
69 err = rdev->ops->join_ibss(&rdev->wiphy, dev, params);
74 memcpy(wdev->ssid, params->ssid, params->ssid_len);
75 wdev->ssid_len = params->ssid_len;
80 void cfg80211_clear_ibss(struct net_device *dev)
82 struct wireless_dev *wdev = dev->ieee80211_ptr;
84 if (wdev->current_bss) {
85 cfg80211_unhold_bss(wdev->current_bss);
86 cfg80211_put_bss(wdev->current_bss);
89 wdev->current_bss = NULL;
91 memset(wdev->bssid, 0, ETH_ALEN);
94 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
95 struct net_device *dev)
99 err = rdev->ops->leave_ibss(&rdev->wiphy, dev);
104 cfg80211_clear_ibss(dev);
109 #ifdef CONFIG_WIRELESS_EXT
110 static int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
111 struct wireless_dev *wdev)
113 enum ieee80211_band band;
116 /* try to find an IBSS channel if none requested ... */
117 if (!wdev->wext.channel) {
118 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
119 struct ieee80211_supported_band *sband;
120 struct ieee80211_channel *chan;
122 sband = rdev->wiphy.bands[band];
126 for (i = 0; i < sband->n_channels; i++) {
127 chan = &sband->channels[i];
128 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
130 if (chan->flags & IEEE80211_CHAN_DISABLED)
132 wdev->wext.channel = chan;
136 if (wdev->wext.channel)
140 if (!wdev->wext.channel)
144 /* don't join -- SSID is not there */
145 if (!wdev->wext.ssid_len)
148 if (!netif_running(wdev->netdev))
151 return cfg80211_join_ibss(wiphy_to_dev(wdev->wiphy),
152 wdev->netdev, &wdev->wext);
155 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
156 struct iw_request_info *info,
157 struct iw_freq *freq, char *extra)
159 struct wireless_dev *wdev = dev->ieee80211_ptr;
160 struct ieee80211_channel *chan;
163 /* call only for ibss! */
164 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
167 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
170 chan = cfg80211_wext_freq(wdev->wiphy, freq);
171 if (chan && IS_ERR(chan))
172 return PTR_ERR(chan);
175 (chan->flags & IEEE80211_CHAN_NO_IBSS ||
176 chan->flags & IEEE80211_CHAN_DISABLED))
179 if (wdev->wext.channel == chan)
182 if (wdev->ssid_len) {
183 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy), dev);
189 wdev->wext.channel = chan;
190 wdev->wext.channel_fixed = true;
192 /* cfg80211_ibss_wext_join will pick one if needed */
193 wdev->wext.channel_fixed = false;
196 return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
198 /* temporary symbol - mark GPL - in the future the handler won't be */
199 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwfreq);
201 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
202 struct iw_request_info *info,
203 struct iw_freq *freq, char *extra)
205 struct wireless_dev *wdev = dev->ieee80211_ptr;
206 struct ieee80211_channel *chan = NULL;
208 /* call only for ibss! */
209 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
212 if (wdev->current_bss)
213 chan = wdev->current_bss->channel;
214 else if (wdev->wext.channel)
215 chan = wdev->wext.channel;
218 freq->m = chan->center_freq;
223 /* no channel if not joining */
226 /* temporary symbol - mark GPL - in the future the handler won't be */
227 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwfreq);
229 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
230 struct iw_request_info *info,
231 struct iw_point *data, char *ssid)
233 struct wireless_dev *wdev = dev->ieee80211_ptr;
234 size_t len = data->length;
237 /* call only for ibss! */
238 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
241 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
244 if (wdev->ssid_len) {
245 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy), dev);
250 /* iwconfig uses nul termination in SSID.. */
251 if (len > 0 && ssid[len - 1] == '\0')
254 wdev->wext.ssid = wdev->ssid;
255 memcpy(wdev->wext.ssid, ssid, len);
256 wdev->wext.ssid_len = len;
258 return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
260 /* temporary symbol - mark GPL - in the future the handler won't be */
261 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwessid);
263 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
264 struct iw_request_info *info,
265 struct iw_point *data, char *ssid)
267 struct wireless_dev *wdev = dev->ieee80211_ptr;
269 /* call only for ibss! */
270 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
275 if (wdev->ssid_len) {
277 data->length = wdev->ssid_len;
278 memcpy(ssid, wdev->ssid, data->length);
279 } else if (wdev->wext.ssid) {
281 data->length = wdev->wext.ssid_len;
282 memcpy(ssid, wdev->wext.ssid, data->length);
287 /* temporary symbol - mark GPL - in the future the handler won't be */
288 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwessid);
290 int cfg80211_ibss_wext_siwap(struct net_device *dev,
291 struct iw_request_info *info,
292 struct sockaddr *ap_addr, char *extra)
294 struct wireless_dev *wdev = dev->ieee80211_ptr;
295 u8 *bssid = ap_addr->sa_data;
298 /* call only for ibss! */
299 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
302 if (!wiphy_to_dev(wdev->wiphy)->ops->join_ibss)
305 if (ap_addr->sa_family != ARPHRD_ETHER)
309 if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
313 if (!bssid && !wdev->wext.bssid)
316 /* fixed already - and no change */
317 if (wdev->wext.bssid && bssid &&
318 compare_ether_addr(bssid, wdev->wext.bssid) == 0)
321 if (wdev->ssid_len) {
322 err = cfg80211_leave_ibss(wiphy_to_dev(wdev->wiphy), dev);
328 memcpy(wdev->wext_bssid, bssid, ETH_ALEN);
329 wdev->wext.bssid = wdev->wext_bssid;
331 wdev->wext.bssid = NULL;
333 return cfg80211_ibss_wext_join(wiphy_to_dev(wdev->wiphy), wdev);
335 /* temporary symbol - mark GPL - in the future the handler won't be */
336 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_siwap);
338 int cfg80211_ibss_wext_giwap(struct net_device *dev,
339 struct iw_request_info *info,
340 struct sockaddr *ap_addr, char *extra)
342 struct wireless_dev *wdev = dev->ieee80211_ptr;
344 /* call only for ibss! */
345 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
348 ap_addr->sa_family = ARPHRD_ETHER;
350 if (wdev->wext.bssid) {
351 memcpy(ap_addr->sa_data, wdev->wext.bssid, ETH_ALEN);
355 memcpy(ap_addr->sa_data, wdev->bssid, ETH_ALEN);
358 /* temporary symbol - mark GPL - in the future the handler won't be */
359 EXPORT_SYMBOL_GPL(cfg80211_ibss_wext_giwap);