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