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