xref: /linux/net/wireless/ibss.c (revision 507e190946297c34a27d9366b0661d5e506fdd03)
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 <linux/slab.h>
10 #include <linux/export.h>
11 #include <net/cfg80211.h>
12 #include "wext-compat.h"
13 #include "nl80211.h"
14 #include "rdev-ops.h"
15 
16 
17 void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
18 			    struct ieee80211_channel *channel)
19 {
20 	struct wireless_dev *wdev = dev->ieee80211_ptr;
21 	struct cfg80211_bss *bss;
22 #ifdef CONFIG_CFG80211_WEXT
23 	union iwreq_data wrqu;
24 #endif
25 
26 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
27 		return;
28 
29 	if (!wdev->ssid_len)
30 		return;
31 
32 	bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
33 			       IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
34 
35 	if (WARN_ON(!bss))
36 		return;
37 
38 	if (wdev->current_bss) {
39 		cfg80211_unhold_bss(wdev->current_bss);
40 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
41 	}
42 
43 	cfg80211_hold_bss(bss_from_pub(bss));
44 	wdev->current_bss = bss_from_pub(bss);
45 
46 	if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
47 		cfg80211_upload_connect_keys(wdev);
48 
49 	nl80211_send_ibss_bssid(wiphy_to_rdev(wdev->wiphy), dev, bssid,
50 				GFP_KERNEL);
51 #ifdef CONFIG_CFG80211_WEXT
52 	memset(&wrqu, 0, sizeof(wrqu));
53 	memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
54 	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
55 #endif
56 }
57 
58 void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
59 			  struct ieee80211_channel *channel, gfp_t gfp)
60 {
61 	struct wireless_dev *wdev = dev->ieee80211_ptr;
62 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
63 	struct cfg80211_event *ev;
64 	unsigned long flags;
65 
66 	trace_cfg80211_ibss_joined(dev, bssid, channel);
67 
68 	if (WARN_ON(!channel))
69 		return;
70 
71 	ev = kzalloc(sizeof(*ev), gfp);
72 	if (!ev)
73 		return;
74 
75 	ev->type = EVENT_IBSS_JOINED;
76 	memcpy(ev->ij.bssid, bssid, ETH_ALEN);
77 	ev->ij.channel = channel;
78 
79 	spin_lock_irqsave(&wdev->event_lock, flags);
80 	list_add_tail(&ev->list, &wdev->event_list);
81 	spin_unlock_irqrestore(&wdev->event_lock, flags);
82 	queue_work(cfg80211_wq, &rdev->event_work);
83 }
84 EXPORT_SYMBOL(cfg80211_ibss_joined);
85 
86 static int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
87 				struct net_device *dev,
88 				struct cfg80211_ibss_params *params,
89 				struct cfg80211_cached_keys *connkeys)
90 {
91 	struct wireless_dev *wdev = dev->ieee80211_ptr;
92 	int err;
93 
94 	ASSERT_WDEV_LOCK(wdev);
95 
96 	if (wdev->ssid_len)
97 		return -EALREADY;
98 
99 	if (!params->basic_rates) {
100 		/*
101 		* If no rates were explicitly configured,
102 		* use the mandatory rate set for 11b or
103 		* 11a for maximum compatibility.
104 		*/
105 		struct ieee80211_supported_band *sband =
106 			rdev->wiphy.bands[params->chandef.chan->band];
107 		int j;
108 		u32 flag = params->chandef.chan->band == NL80211_BAND_5GHZ ?
109 			IEEE80211_RATE_MANDATORY_A :
110 			IEEE80211_RATE_MANDATORY_B;
111 
112 		for (j = 0; j < sband->n_bitrates; j++) {
113 			if (sband->bitrates[j].flags & flag)
114 				params->basic_rates |= BIT(j);
115 		}
116 	}
117 
118 	if (WARN_ON(connkeys && connkeys->def < 0))
119 		return -EINVAL;
120 
121 	if (WARN_ON(wdev->connect_keys))
122 		kzfree(wdev->connect_keys);
123 	wdev->connect_keys = connkeys;
124 
125 	wdev->ibss_fixed = params->channel_fixed;
126 	wdev->ibss_dfs_possible = params->userspace_handles_dfs;
127 	wdev->chandef = params->chandef;
128 #ifdef CONFIG_CFG80211_WEXT
129 	wdev->wext.ibss.chandef = params->chandef;
130 #endif
131 	err = rdev_join_ibss(rdev, dev, params);
132 	if (err) {
133 		wdev->connect_keys = NULL;
134 		return err;
135 	}
136 
137 	memcpy(wdev->ssid, params->ssid, params->ssid_len);
138 	wdev->ssid_len = params->ssid_len;
139 
140 	return 0;
141 }
142 
143 int cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
144 		       struct net_device *dev,
145 		       struct cfg80211_ibss_params *params,
146 		       struct cfg80211_cached_keys *connkeys)
147 {
148 	struct wireless_dev *wdev = dev->ieee80211_ptr;
149 	int err;
150 
151 	ASSERT_RTNL();
152 
153 	wdev_lock(wdev);
154 	err = __cfg80211_join_ibss(rdev, dev, params, connkeys);
155 	wdev_unlock(wdev);
156 
157 	return err;
158 }
159 
160 static void __cfg80211_clear_ibss(struct net_device *dev, bool nowext)
161 {
162 	struct wireless_dev *wdev = dev->ieee80211_ptr;
163 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
164 	int i;
165 
166 	ASSERT_WDEV_LOCK(wdev);
167 
168 	kzfree(wdev->connect_keys);
169 	wdev->connect_keys = NULL;
170 
171 	rdev_set_qos_map(rdev, dev, NULL);
172 
173 	/*
174 	 * Delete all the keys ... pairwise keys can't really
175 	 * exist any more anyway, but default keys might.
176 	 */
177 	if (rdev->ops->del_key)
178 		for (i = 0; i < 6; i++)
179 			rdev_del_key(rdev, dev, i, false, NULL);
180 
181 	if (wdev->current_bss) {
182 		cfg80211_unhold_bss(wdev->current_bss);
183 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
184 	}
185 
186 	wdev->current_bss = NULL;
187 	wdev->ssid_len = 0;
188 	memset(&wdev->chandef, 0, sizeof(wdev->chandef));
189 #ifdef CONFIG_CFG80211_WEXT
190 	if (!nowext)
191 		wdev->wext.ibss.ssid_len = 0;
192 #endif
193 	cfg80211_sched_dfs_chan_update(rdev);
194 }
195 
196 void cfg80211_clear_ibss(struct net_device *dev, bool nowext)
197 {
198 	struct wireless_dev *wdev = dev->ieee80211_ptr;
199 
200 	wdev_lock(wdev);
201 	__cfg80211_clear_ibss(dev, nowext);
202 	wdev_unlock(wdev);
203 }
204 
205 int __cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
206 			  struct net_device *dev, bool nowext)
207 {
208 	struct wireless_dev *wdev = dev->ieee80211_ptr;
209 	int err;
210 
211 	ASSERT_WDEV_LOCK(wdev);
212 
213 	if (!wdev->ssid_len)
214 		return -ENOLINK;
215 
216 	err = rdev_leave_ibss(rdev, dev);
217 
218 	if (err)
219 		return err;
220 
221 	__cfg80211_clear_ibss(dev, nowext);
222 
223 	return 0;
224 }
225 
226 int cfg80211_leave_ibss(struct cfg80211_registered_device *rdev,
227 			struct net_device *dev, bool nowext)
228 {
229 	struct wireless_dev *wdev = dev->ieee80211_ptr;
230 	int err;
231 
232 	wdev_lock(wdev);
233 	err = __cfg80211_leave_ibss(rdev, dev, nowext);
234 	wdev_unlock(wdev);
235 
236 	return err;
237 }
238 
239 #ifdef CONFIG_CFG80211_WEXT
240 int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
241 			    struct wireless_dev *wdev)
242 {
243 	struct cfg80211_cached_keys *ck = NULL;
244 	enum nl80211_band band;
245 	int i, err;
246 
247 	ASSERT_WDEV_LOCK(wdev);
248 
249 	if (!wdev->wext.ibss.beacon_interval)
250 		wdev->wext.ibss.beacon_interval = 100;
251 
252 	/* try to find an IBSS channel if none requested ... */
253 	if (!wdev->wext.ibss.chandef.chan) {
254 		struct ieee80211_channel *new_chan = NULL;
255 
256 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
257 			struct ieee80211_supported_band *sband;
258 			struct ieee80211_channel *chan;
259 
260 			sband = rdev->wiphy.bands[band];
261 			if (!sband)
262 				continue;
263 
264 			for (i = 0; i < sband->n_channels; i++) {
265 				chan = &sband->channels[i];
266 				if (chan->flags & IEEE80211_CHAN_NO_IR)
267 					continue;
268 				if (chan->flags & IEEE80211_CHAN_DISABLED)
269 					continue;
270 				new_chan = chan;
271 				break;
272 			}
273 
274 			if (new_chan)
275 				break;
276 		}
277 
278 		if (!new_chan)
279 			return -EINVAL;
280 
281 		cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
282 					NL80211_CHAN_NO_HT);
283 	}
284 
285 	/* don't join -- SSID is not there */
286 	if (!wdev->wext.ibss.ssid_len)
287 		return 0;
288 
289 	if (!netif_running(wdev->netdev))
290 		return 0;
291 
292 	if (wdev->wext.keys)
293 		wdev->wext.keys->def = wdev->wext.default_key;
294 
295 	wdev->wext.ibss.privacy = wdev->wext.default_key != -1;
296 
297 	if (wdev->wext.keys && wdev->wext.keys->def != -1) {
298 		ck = kmemdup(wdev->wext.keys, sizeof(*ck), GFP_KERNEL);
299 		if (!ck)
300 			return -ENOMEM;
301 		for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
302 			ck->params[i].key = ck->data[i];
303 	}
304 	err = __cfg80211_join_ibss(rdev, wdev->netdev,
305 				   &wdev->wext.ibss, ck);
306 	if (err)
307 		kfree(ck);
308 
309 	return err;
310 }
311 
312 int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
313 			       struct iw_request_info *info,
314 			       struct iw_freq *wextfreq, char *extra)
315 {
316 	struct wireless_dev *wdev = dev->ieee80211_ptr;
317 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
318 	struct ieee80211_channel *chan = NULL;
319 	int err, freq;
320 
321 	/* call only for ibss! */
322 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
323 		return -EINVAL;
324 
325 	if (!rdev->ops->join_ibss)
326 		return -EOPNOTSUPP;
327 
328 	freq = cfg80211_wext_freq(wextfreq);
329 	if (freq < 0)
330 		return freq;
331 
332 	if (freq) {
333 		chan = ieee80211_get_channel(wdev->wiphy, freq);
334 		if (!chan)
335 			return -EINVAL;
336 		if (chan->flags & IEEE80211_CHAN_NO_IR ||
337 		    chan->flags & IEEE80211_CHAN_DISABLED)
338 			return -EINVAL;
339 	}
340 
341 	if (wdev->wext.ibss.chandef.chan == chan)
342 		return 0;
343 
344 	wdev_lock(wdev);
345 	err = 0;
346 	if (wdev->ssid_len)
347 		err = __cfg80211_leave_ibss(rdev, dev, true);
348 	wdev_unlock(wdev);
349 
350 	if (err)
351 		return err;
352 
353 	if (chan) {
354 		cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
355 					NL80211_CHAN_NO_HT);
356 		wdev->wext.ibss.channel_fixed = true;
357 	} else {
358 		/* cfg80211_ibss_wext_join will pick one if needed */
359 		wdev->wext.ibss.channel_fixed = false;
360 	}
361 
362 	wdev_lock(wdev);
363 	err = cfg80211_ibss_wext_join(rdev, wdev);
364 	wdev_unlock(wdev);
365 
366 	return err;
367 }
368 
369 int cfg80211_ibss_wext_giwfreq(struct net_device *dev,
370 			       struct iw_request_info *info,
371 			       struct iw_freq *freq, char *extra)
372 {
373 	struct wireless_dev *wdev = dev->ieee80211_ptr;
374 	struct ieee80211_channel *chan = NULL;
375 
376 	/* call only for ibss! */
377 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
378 		return -EINVAL;
379 
380 	wdev_lock(wdev);
381 	if (wdev->current_bss)
382 		chan = wdev->current_bss->pub.channel;
383 	else if (wdev->wext.ibss.chandef.chan)
384 		chan = wdev->wext.ibss.chandef.chan;
385 	wdev_unlock(wdev);
386 
387 	if (chan) {
388 		freq->m = chan->center_freq;
389 		freq->e = 6;
390 		return 0;
391 	}
392 
393 	/* no channel if not joining */
394 	return -EINVAL;
395 }
396 
397 int cfg80211_ibss_wext_siwessid(struct net_device *dev,
398 				struct iw_request_info *info,
399 				struct iw_point *data, char *ssid)
400 {
401 	struct wireless_dev *wdev = dev->ieee80211_ptr;
402 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
403 	size_t len = data->length;
404 	int err;
405 
406 	/* call only for ibss! */
407 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
408 		return -EINVAL;
409 
410 	if (!rdev->ops->join_ibss)
411 		return -EOPNOTSUPP;
412 
413 	wdev_lock(wdev);
414 	err = 0;
415 	if (wdev->ssid_len)
416 		err = __cfg80211_leave_ibss(rdev, dev, true);
417 	wdev_unlock(wdev);
418 
419 	if (err)
420 		return err;
421 
422 	/* iwconfig uses nul termination in SSID.. */
423 	if (len > 0 && ssid[len - 1] == '\0')
424 		len--;
425 
426 	memcpy(wdev->ssid, ssid, len);
427 	wdev->wext.ibss.ssid = wdev->ssid;
428 	wdev->wext.ibss.ssid_len = len;
429 
430 	wdev_lock(wdev);
431 	err = cfg80211_ibss_wext_join(rdev, wdev);
432 	wdev_unlock(wdev);
433 
434 	return err;
435 }
436 
437 int cfg80211_ibss_wext_giwessid(struct net_device *dev,
438 				struct iw_request_info *info,
439 				struct iw_point *data, char *ssid)
440 {
441 	struct wireless_dev *wdev = dev->ieee80211_ptr;
442 
443 	/* call only for ibss! */
444 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
445 		return -EINVAL;
446 
447 	data->flags = 0;
448 
449 	wdev_lock(wdev);
450 	if (wdev->ssid_len) {
451 		data->flags = 1;
452 		data->length = wdev->ssid_len;
453 		memcpy(ssid, wdev->ssid, data->length);
454 	} else if (wdev->wext.ibss.ssid && wdev->wext.ibss.ssid_len) {
455 		data->flags = 1;
456 		data->length = wdev->wext.ibss.ssid_len;
457 		memcpy(ssid, wdev->wext.ibss.ssid, data->length);
458 	}
459 	wdev_unlock(wdev);
460 
461 	return 0;
462 }
463 
464 int cfg80211_ibss_wext_siwap(struct net_device *dev,
465 			     struct iw_request_info *info,
466 			     struct sockaddr *ap_addr, char *extra)
467 {
468 	struct wireless_dev *wdev = dev->ieee80211_ptr;
469 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
470 	u8 *bssid = ap_addr->sa_data;
471 	int err;
472 
473 	/* call only for ibss! */
474 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
475 		return -EINVAL;
476 
477 	if (!rdev->ops->join_ibss)
478 		return -EOPNOTSUPP;
479 
480 	if (ap_addr->sa_family != ARPHRD_ETHER)
481 		return -EINVAL;
482 
483 	/* automatic mode */
484 	if (is_zero_ether_addr(bssid) || is_broadcast_ether_addr(bssid))
485 		bssid = NULL;
486 
487 	if (bssid && !is_valid_ether_addr(bssid))
488 		return -EINVAL;
489 
490 	/* both automatic */
491 	if (!bssid && !wdev->wext.ibss.bssid)
492 		return 0;
493 
494 	/* fixed already - and no change */
495 	if (wdev->wext.ibss.bssid && bssid &&
496 	    ether_addr_equal(bssid, wdev->wext.ibss.bssid))
497 		return 0;
498 
499 	wdev_lock(wdev);
500 	err = 0;
501 	if (wdev->ssid_len)
502 		err = __cfg80211_leave_ibss(rdev, dev, true);
503 	wdev_unlock(wdev);
504 
505 	if (err)
506 		return err;
507 
508 	if (bssid) {
509 		memcpy(wdev->wext.bssid, bssid, ETH_ALEN);
510 		wdev->wext.ibss.bssid = wdev->wext.bssid;
511 	} else
512 		wdev->wext.ibss.bssid = NULL;
513 
514 	wdev_lock(wdev);
515 	err = cfg80211_ibss_wext_join(rdev, wdev);
516 	wdev_unlock(wdev);
517 
518 	return err;
519 }
520 
521 int cfg80211_ibss_wext_giwap(struct net_device *dev,
522 			     struct iw_request_info *info,
523 			     struct sockaddr *ap_addr, char *extra)
524 {
525 	struct wireless_dev *wdev = dev->ieee80211_ptr;
526 
527 	/* call only for ibss! */
528 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_ADHOC))
529 		return -EINVAL;
530 
531 	ap_addr->sa_family = ARPHRD_ETHER;
532 
533 	wdev_lock(wdev);
534 	if (wdev->current_bss)
535 		memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
536 	else if (wdev->wext.ibss.bssid)
537 		memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
538 	else
539 		eth_zero_addr(ap_addr->sa_data);
540 
541 	wdev_unlock(wdev);
542 
543 	return 0;
544 }
545 #endif
546