xref: /linux/net/wireless/tests/scan.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * KUnit tests for inform_bss functions
4  *
5  * Copyright (C) 2023-2024 Intel Corporation
6  */
7 #include <linux/ieee80211.h>
8 #include <net/cfg80211.h>
9 #include <kunit/test.h>
10 #include <kunit/skbuff.h>
11 #include "../core.h"
12 #include "util.h"
13 
14 /* mac80211 helpers for element building */
15 #include "../../mac80211/ieee80211_i.h"
16 
17 MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
18 
19 struct test_elem {
20 	u8 id;
21 	u8 len;
22 	union {
23 		u8 data[255];
24 		struct {
25 			u8 eid;
26 			u8 edata[254];
27 		};
28 	};
29 };
30 
31 static struct gen_new_ie_case {
32 	const char *desc;
33 	struct test_elem parent_ies[16];
34 	struct test_elem child_ies[16];
35 	struct test_elem result_ies[16];
36 } gen_new_ie_cases[] = {
37 	{
38 		.desc = "ML not inherited",
39 		.parent_ies = {
40 			{ .id = WLAN_EID_EXTENSION, .len = 255,
41 			  .eid = WLAN_EID_EXT_EHT_MULTI_LINK },
42 		},
43 		.child_ies = {
44 			{ .id = WLAN_EID_SSID, .len = 2 },
45 		},
46 		.result_ies = {
47 			{ .id = WLAN_EID_SSID, .len = 2 },
48 		},
49 	},
50 	{
51 		.desc = "fragments are ignored if previous len not 255",
52 		.parent_ies = {
53 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 254, },
54 			{ .id = WLAN_EID_FRAGMENT, .len = 125, },
55 		},
56 		.child_ies = {
57 			{ .id = WLAN_EID_SSID, .len = 2 },
58 			{ .id = WLAN_EID_FRAGMENT, .len = 125, },
59 		},
60 		.result_ies = {
61 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 254, },
62 			{ .id = WLAN_EID_SSID, .len = 2 },
63 		},
64 	},
65 	{
66 		.desc = "fragments inherited",
67 		.parent_ies = {
68 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 255, },
69 			{ .id = WLAN_EID_FRAGMENT, .len = 125, },
70 		},
71 		.child_ies = {
72 			{ .id = WLAN_EID_SSID, .len = 2 },
73 		},
74 		.result_ies = {
75 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 255, },
76 			{ .id = WLAN_EID_FRAGMENT, .len = 125, },
77 			{ .id = WLAN_EID_SSID, .len = 2 },
78 		},
79 	},
80 	{
81 		.desc = "fragments copied",
82 		.parent_ies = {
83 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 255, },
84 			{ .id = WLAN_EID_FRAGMENT, .len = 125, },
85 		},
86 		.child_ies = {
87 			{ .id = WLAN_EID_SSID, .len = 2 },
88 		},
89 		.result_ies = {
90 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 255, },
91 			{ .id = WLAN_EID_FRAGMENT, .len = 125, },
92 			{ .id = WLAN_EID_SSID, .len = 2 },
93 		},
94 	},
95 	{
96 		.desc = "multiple elements inherit",
97 		.parent_ies = {
98 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 255, },
99 			{ .id = WLAN_EID_FRAGMENT, .len = 125, },
100 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 123, },
101 		},
102 		.child_ies = {
103 			{ .id = WLAN_EID_SSID, .len = 2 },
104 		},
105 		.result_ies = {
106 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 255, },
107 			{ .id = WLAN_EID_FRAGMENT, .len = 125, },
108 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 123, },
109 			{ .id = WLAN_EID_SSID, .len = 2 },
110 		},
111 	},
112 	{
113 		.desc = "one child element overrides",
114 		.parent_ies = {
115 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 255, },
116 			{ .id = WLAN_EID_FRAGMENT, .len = 125, },
117 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 123, },
118 		},
119 		.child_ies = {
120 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 127, },
121 			{ .id = WLAN_EID_SSID, .len = 2 },
122 		},
123 		.result_ies = {
124 			{ .id = WLAN_EID_REDUCED_NEIGHBOR_REPORT, .len = 127, },
125 			{ .id = WLAN_EID_SSID, .len = 2 },
126 		},
127 	},
128 	{
129 		.desc = "empty elements from parent",
130 		.parent_ies = {
131 			{ .id = 0x1, .len = 0, },
132 			{ .id = WLAN_EID_EXTENSION, .len = 1, .eid = 0x10 },
133 		},
134 		.child_ies = {
135 		},
136 		.result_ies = {
137 			{ .id = 0x1, .len = 0, },
138 			{ .id = WLAN_EID_EXTENSION, .len = 1, .eid = 0x10 },
139 		},
140 	},
141 	{
142 		.desc = "empty elements from child",
143 		.parent_ies = {
144 		},
145 		.child_ies = {
146 			{ .id = 0x1, .len = 0, },
147 			{ .id = WLAN_EID_EXTENSION, .len = 1, .eid = 0x10 },
148 		},
149 		.result_ies = {
150 			{ .id = 0x1, .len = 0, },
151 			{ .id = WLAN_EID_EXTENSION, .len = 1, .eid = 0x10 },
152 		},
153 	},
154 	{
155 		.desc = "invalid extended elements ignored",
156 		.parent_ies = {
157 			{ .id = WLAN_EID_EXTENSION, .len = 0 },
158 		},
159 		.child_ies = {
160 			{ .id = WLAN_EID_EXTENSION, .len = 0 },
161 		},
162 		.result_ies = {
163 		},
164 	},
165 	{
166 		.desc = "multiple extended elements",
167 		.parent_ies = {
168 			{ .id = WLAN_EID_EXTENSION, .len = 3,
169 			  .eid = WLAN_EID_EXT_HE_CAPABILITY },
170 			{ .id = WLAN_EID_EXTENSION, .len = 5,
171 			  .eid = WLAN_EID_EXT_ASSOC_DELAY_INFO },
172 			{ .id = WLAN_EID_EXTENSION, .len = 7,
173 			  .eid = WLAN_EID_EXT_HE_OPERATION },
174 			{ .id = WLAN_EID_EXTENSION, .len = 11,
175 			  .eid = WLAN_EID_EXT_FILS_REQ_PARAMS },
176 		},
177 		.child_ies = {
178 			{ .id = WLAN_EID_SSID, .len = 13 },
179 			{ .id = WLAN_EID_EXTENSION, .len = 17,
180 			  .eid = WLAN_EID_EXT_HE_CAPABILITY },
181 			{ .id = WLAN_EID_EXTENSION, .len = 11,
182 			  .eid = WLAN_EID_EXT_FILS_KEY_CONFIRM },
183 			{ .id = WLAN_EID_EXTENSION, .len = 19,
184 			  .eid = WLAN_EID_EXT_HE_OPERATION },
185 		},
186 		.result_ies = {
187 			{ .id = WLAN_EID_EXTENSION, .len = 17,
188 			  .eid = WLAN_EID_EXT_HE_CAPABILITY },
189 			{ .id = WLAN_EID_EXTENSION, .len = 5,
190 			  .eid = WLAN_EID_EXT_ASSOC_DELAY_INFO },
191 			{ .id = WLAN_EID_EXTENSION, .len = 19,
192 			  .eid = WLAN_EID_EXT_HE_OPERATION },
193 			{ .id = WLAN_EID_EXTENSION, .len = 11,
194 			  .eid = WLAN_EID_EXT_FILS_REQ_PARAMS },
195 			{ .id = WLAN_EID_SSID, .len = 13 },
196 			{ .id = WLAN_EID_EXTENSION, .len = 11,
197 			  .eid = WLAN_EID_EXT_FILS_KEY_CONFIRM },
198 		},
199 	},
200 	{
201 		.desc = "non-inherit element",
202 		.parent_ies = {
203 			{ .id = 0x1, .len = 7, },
204 			{ .id = 0x2, .len = 11, },
205 			{ .id = 0x3, .len = 13, },
206 			{ .id = WLAN_EID_EXTENSION, .len = 17, .eid = 0x10 },
207 			{ .id = WLAN_EID_EXTENSION, .len = 19, .eid = 0x11 },
208 			{ .id = WLAN_EID_EXTENSION, .len = 23, .eid = 0x12 },
209 			{ .id = WLAN_EID_EXTENSION, .len = 29, .eid = 0x14 },
210 		},
211 		.child_ies = {
212 			{ .id = WLAN_EID_EXTENSION,
213 			  .eid = WLAN_EID_EXT_NON_INHERITANCE,
214 			  .len = 10,
215 			  .edata = { 0x3, 0x1, 0x2, 0x3,
216 				     0x4, 0x10, 0x11, 0x13, 0x14 } },
217 			{ .id = WLAN_EID_SSID, .len = 2 },
218 		},
219 		.result_ies = {
220 			{ .id = WLAN_EID_EXTENSION, .len = 23, .eid = 0x12 },
221 			{ .id = WLAN_EID_SSID, .len = 2 },
222 		},
223 	},
224 };
KUNIT_ARRAY_PARAM_DESC(gen_new_ie,gen_new_ie_cases,desc)225 KUNIT_ARRAY_PARAM_DESC(gen_new_ie, gen_new_ie_cases, desc)
226 
227 static void test_gen_new_ie(struct kunit *test)
228 {
229 	const struct gen_new_ie_case *params = test->param_value;
230 	struct sk_buff *parent = kunit_zalloc_skb(test, 1024, GFP_KERNEL);
231 	struct sk_buff *child = kunit_zalloc_skb(test, 1024, GFP_KERNEL);
232 	struct sk_buff *reference = kunit_zalloc_skb(test, 1024, GFP_KERNEL);
233 	u8 *out = kunit_kzalloc(test, IEEE80211_MAX_DATA_LEN, GFP_KERNEL);
234 	size_t len;
235 	int i;
236 
237 	KUNIT_ASSERT_NOT_NULL(test, parent);
238 	KUNIT_ASSERT_NOT_NULL(test, child);
239 	KUNIT_ASSERT_NOT_NULL(test, reference);
240 	KUNIT_ASSERT_NOT_NULL(test, out);
241 
242 	for (i = 0; i < ARRAY_SIZE(params->parent_ies); i++) {
243 		if (params->parent_ies[i].len != 0) {
244 			skb_put_u8(parent, params->parent_ies[i].id);
245 			skb_put_u8(parent, params->parent_ies[i].len);
246 			skb_put_data(parent, params->parent_ies[i].data,
247 				     params->parent_ies[i].len);
248 		}
249 
250 		if (params->child_ies[i].len != 0) {
251 			skb_put_u8(child, params->child_ies[i].id);
252 			skb_put_u8(child, params->child_ies[i].len);
253 			skb_put_data(child, params->child_ies[i].data,
254 				     params->child_ies[i].len);
255 		}
256 
257 		if (params->result_ies[i].len != 0) {
258 			skb_put_u8(reference, params->result_ies[i].id);
259 			skb_put_u8(reference, params->result_ies[i].len);
260 			skb_put_data(reference, params->result_ies[i].data,
261 				     params->result_ies[i].len);
262 		}
263 	}
264 
265 	len = cfg80211_gen_new_ie(parent->data, parent->len,
266 				  child->data, child->len,
267 				  out, IEEE80211_MAX_DATA_LEN);
268 	KUNIT_EXPECT_EQ(test, len, reference->len);
269 	KUNIT_EXPECT_MEMEQ(test, out, reference->data, reference->len);
270 	memset(out, 0, IEEE80211_MAX_DATA_LEN);
271 
272 	/* Exactly enough space */
273 	len = cfg80211_gen_new_ie(parent->data, parent->len,
274 				  child->data, child->len,
275 				  out, reference->len);
276 	KUNIT_EXPECT_EQ(test, len, reference->len);
277 	KUNIT_EXPECT_MEMEQ(test, out, reference->data, reference->len);
278 	memset(out, 0, IEEE80211_MAX_DATA_LEN);
279 
280 	/* Not enough space (or expected zero length) */
281 	len = cfg80211_gen_new_ie(parent->data, parent->len,
282 				  child->data, child->len,
283 				  out, reference->len - 1);
284 	KUNIT_EXPECT_EQ(test, len, 0);
285 }
286 
test_gen_new_ie_malformed(struct kunit * test)287 static void test_gen_new_ie_malformed(struct kunit *test)
288 {
289 	struct sk_buff *malformed = kunit_zalloc_skb(test, 1024, GFP_KERNEL);
290 	u8 *out = kunit_kzalloc(test, IEEE80211_MAX_DATA_LEN, GFP_KERNEL);
291 	size_t len;
292 
293 	KUNIT_ASSERT_NOT_NULL(test, malformed);
294 	KUNIT_ASSERT_NOT_NULL(test, out);
295 
296 	skb_put_u8(malformed, WLAN_EID_SSID);
297 	skb_put_u8(malformed, 3);
298 	skb_put(malformed, 3);
299 	skb_put_u8(malformed, WLAN_EID_REDUCED_NEIGHBOR_REPORT);
300 	skb_put_u8(malformed, 10);
301 	skb_put(malformed, 9);
302 
303 	len = cfg80211_gen_new_ie(malformed->data, malformed->len,
304 				  out, 0,
305 				  out, IEEE80211_MAX_DATA_LEN);
306 	KUNIT_EXPECT_EQ(test, len, 5);
307 
308 	len = cfg80211_gen_new_ie(out, 0,
309 				  malformed->data, malformed->len,
310 				  out, IEEE80211_MAX_DATA_LEN);
311 	KUNIT_EXPECT_EQ(test, len, 5);
312 }
313 
314 struct inform_bss {
315 	struct kunit *test;
316 
317 	int inform_bss_count;
318 };
319 
inform_bss_inc_counter(struct wiphy * wiphy,struct cfg80211_bss * bss,const struct cfg80211_bss_ies * ies,void * drv_data)320 static void inform_bss_inc_counter(struct wiphy *wiphy,
321 				   struct cfg80211_bss *bss,
322 				   const struct cfg80211_bss_ies *ies,
323 				   void *drv_data)
324 {
325 	struct inform_bss *ctx = t_wiphy_ctx(wiphy);
326 
327 	ctx->inform_bss_count++;
328 
329 	rcu_read_lock();
330 	KUNIT_EXPECT_PTR_EQ(ctx->test, drv_data, ctx);
331 	KUNIT_EXPECT_PTR_EQ(ctx->test, ies, rcu_dereference(bss->ies));
332 	rcu_read_unlock();
333 }
334 
test_inform_bss_ssid_only(struct kunit * test)335 static void test_inform_bss_ssid_only(struct kunit *test)
336 {
337 	struct inform_bss ctx = {
338 		.test = test,
339 	};
340 	struct wiphy *wiphy = T_WIPHY(test, ctx);
341 	struct t_wiphy_priv *w_priv = wiphy_priv(wiphy);
342 	struct cfg80211_inform_bss inform_bss = {
343 		.signal = 50,
344 		.drv_data = &ctx,
345 	};
346 	const u8 bssid[ETH_ALEN] = { 0x10, 0x22, 0x33, 0x44, 0x55, 0x66 };
347 	u64 tsf = 0x1000000000000000ULL;
348 	int beacon_int = 100;
349 	u16 capability = 0x1234;
350 	static const u8 input[] = {
351 		[0] = WLAN_EID_SSID,
352 		[1] = 4,
353 		[2] = 'T', 'E', 'S', 'T'
354 	};
355 	struct cfg80211_bss *bss, *other;
356 	const struct cfg80211_bss_ies *ies;
357 
358 	w_priv->ops->inform_bss = inform_bss_inc_counter;
359 
360 	inform_bss.chan = ieee80211_get_channel_khz(wiphy, MHZ_TO_KHZ(2412));
361 	KUNIT_ASSERT_NOT_NULL(test, inform_bss.chan);
362 
363 	bss = cfg80211_inform_bss_data(wiphy, &inform_bss,
364 				       CFG80211_BSS_FTYPE_PRESP, bssid, tsf,
365 				       capability, beacon_int,
366 				       input, sizeof(input),
367 				       GFP_KERNEL);
368 	KUNIT_EXPECT_NOT_NULL(test, bss);
369 	KUNIT_EXPECT_EQ(test, ctx.inform_bss_count, 1);
370 
371 	/* Check values in returned bss are correct */
372 	KUNIT_EXPECT_EQ(test, bss->signal, inform_bss.signal);
373 	KUNIT_EXPECT_EQ(test, bss->beacon_interval, beacon_int);
374 	KUNIT_EXPECT_EQ(test, bss->capability, capability);
375 	KUNIT_EXPECT_EQ(test, bss->bssid_index, 0);
376 	KUNIT_EXPECT_PTR_EQ(test, bss->channel, inform_bss.chan);
377 	KUNIT_EXPECT_MEMEQ(test, bssid, bss->bssid, sizeof(bssid));
378 
379 	/* Check the IEs have the expected value */
380 	rcu_read_lock();
381 	ies = rcu_dereference(bss->ies);
382 	KUNIT_EXPECT_NOT_NULL(test, ies);
383 	KUNIT_EXPECT_EQ(test, ies->tsf, tsf);
384 	KUNIT_EXPECT_EQ(test, ies->len, sizeof(input));
385 	KUNIT_EXPECT_MEMEQ(test, ies->data, input, sizeof(input));
386 	rcu_read_unlock();
387 
388 	/* Check we can look up the BSS - by SSID */
389 	other = cfg80211_get_bss(wiphy, NULL, NULL, "TEST", 4,
390 				 IEEE80211_BSS_TYPE_ANY,
391 				 IEEE80211_PRIVACY_ANY);
392 	KUNIT_EXPECT_PTR_EQ(test, bss, other);
393 	cfg80211_put_bss(wiphy, other);
394 
395 	/* Check we can look up the BSS - by BSSID */
396 	other = cfg80211_get_bss(wiphy, NULL, bssid, NULL, 0,
397 				 IEEE80211_BSS_TYPE_ANY,
398 				 IEEE80211_PRIVACY_ANY);
399 	KUNIT_EXPECT_PTR_EQ(test, bss, other);
400 	cfg80211_put_bss(wiphy, other);
401 
402 	cfg80211_put_bss(wiphy, bss);
403 }
404 
test_get_bss_miss_reason(struct kunit * test)405 static void test_get_bss_miss_reason(struct kunit *test)
406 {
407 	struct inform_bss ctx = {
408 		.test = test,
409 	};
410 	struct wiphy *wiphy = T_WIPHY(test, ctx);
411 	struct cfg80211_inform_bss inform_bss = {
412 		.signal = 50,
413 		.drv_data = &ctx,
414 	};
415 	const u8 bssid[ETH_ALEN] = { 0x10, 0x22, 0x33, 0x44, 0x55, 0x66 };
416 	const u8 other_bssid[ETH_ALEN] = { 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
417 	static const u8 ies[] = {
418 		[0] = WLAN_EID_SSID,
419 		[1] = 4,
420 		[2] = 'T', 'E', 'S', 'T'
421 	};
422 	struct cfg80211_internal_bss *ibss;
423 	struct netlink_ext_ack extack = {};
424 	struct cfg80211_bss *bss, *bss2, *found;
425 
426 	inform_bss.chan = ieee80211_get_channel_khz(wiphy, MHZ_TO_KHZ(2412));
427 	KUNIT_ASSERT_NOT_NULL(test, inform_bss.chan);
428 
429 	bss = cfg80211_inform_bss_data(wiphy, &inform_bss,
430 				       CFG80211_BSS_FTYPE_PRESP, bssid, 0,
431 				       0x1234, 100, ies, sizeof(ies),
432 				       GFP_KERNEL);
433 	KUNIT_ASSERT_NOT_NULL(test, bss);
434 	ibss = container_of(bss, struct cfg80211_internal_bss, pub);
435 
436 	/* Fresh usable entry: found, no message is set */
437 	found = __cfg80211_get_bss(wiphy, NULL, bssid, NULL, 0,
438 				   IEEE80211_BSS_TYPE_ANY,
439 				   IEEE80211_PRIVACY_ANY,
440 				   NL80211_BSS_USE_FOR_NORMAL, &extack);
441 	KUNIT_ASSERT_PTR_EQ(test, found, bss);
442 	KUNIT_EXPECT_NULL(test, extack._msg);
443 	cfg80211_put_bss(wiphy, found);
444 
445 	/* No entry at all for this BSSID */
446 	found = __cfg80211_get_bss(wiphy, NULL, other_bssid, NULL, 0,
447 				   IEEE80211_BSS_TYPE_ANY,
448 				   IEEE80211_PRIVACY_ANY,
449 				   NL80211_BSS_USE_FOR_NORMAL, &extack);
450 	KUNIT_EXPECT_NULL(test, found);
451 	KUNIT_EXPECT_STREQ(test, extack._msg, "BSS not found in scan results");
452 
453 	/* Fresh entry that is not usable for the requested use */
454 	extack._msg = NULL;
455 	bss->use_for = 0;
456 	found = __cfg80211_get_bss(wiphy, NULL, bssid, NULL, 0,
457 				   IEEE80211_BSS_TYPE_ANY,
458 				   IEEE80211_PRIVACY_ANY,
459 				   NL80211_BSS_USE_FOR_NORMAL, &extack);
460 	KUNIT_EXPECT_NULL(test, found);
461 	KUNIT_EXPECT_STREQ(test, extack._msg,
462 			   "BSS cannot be used for the requested operation");
463 	bss->use_for = NL80211_BSS_USE_FOR_ALL;
464 
465 	/* Expired entry, > IEEE80211_SCAN_RESULT_EXPIRE (30s) old */
466 	extack._msg = NULL;
467 	ibss->ts = jiffies - 60 * HZ;
468 	found = __cfg80211_get_bss(wiphy, NULL, bssid, NULL, 0,
469 				   IEEE80211_BSS_TYPE_ANY,
470 				   IEEE80211_PRIVACY_ANY,
471 				   NL80211_BSS_USE_FOR_NORMAL, &extack);
472 	KUNIT_EXPECT_NULL(test, found);
473 	KUNIT_EXPECT_STREQ(test, extack._msg,
474 			   "BSS entry in scan results is expired");
475 
476 	/* An entry both expired and unusable reports expired */
477 	extack._msg = NULL;
478 	bss->use_for = 0;
479 	found = __cfg80211_get_bss(wiphy, NULL, bssid, NULL, 0,
480 				   IEEE80211_BSS_TYPE_ANY,
481 				   IEEE80211_PRIVACY_ANY,
482 				   NL80211_BSS_USE_FOR_NORMAL, &extack);
483 	KUNIT_EXPECT_NULL(test, found);
484 	KUNIT_EXPECT_STREQ(test, extack._msg,
485 			   "BSS entry in scan results is expired");
486 	bss->use_for = NL80211_BSS_USE_FOR_ALL;
487 
488 	/* Expired but held entries are still usable, no message is set */
489 	extack._msg = NULL;
490 	atomic_set(&ibss->hold, 1);
491 	found = __cfg80211_get_bss(wiphy, NULL, bssid, NULL, 0,
492 				   IEEE80211_BSS_TYPE_ANY,
493 				   IEEE80211_PRIVACY_ANY,
494 				   NL80211_BSS_USE_FOR_NORMAL, &extack);
495 	KUNIT_ASSERT_PTR_EQ(test, found, bss);
496 	KUNIT_EXPECT_NULL(test, extack._msg);
497 	cfg80211_put_bss(wiphy, found);
498 	atomic_set(&ibss->hold, 0);
499 
500 	/*
501 	 * With one matching entry expired and another current but
502 	 * unusable, both reasons are reported.
503 	 */
504 	bss2 = cfg80211_inform_bss_data(wiphy, &inform_bss,
505 					CFG80211_BSS_FTYPE_PRESP, other_bssid,
506 					0, 0x1234, 100, ies, sizeof(ies),
507 					GFP_KERNEL);
508 	KUNIT_ASSERT_NOT_NULL(test, bss2);
509 	bss2->use_for = 0;
510 	extack._msg = NULL;
511 	found = __cfg80211_get_bss(wiphy, NULL, NULL, "TEST", 4,
512 				   IEEE80211_BSS_TYPE_ANY,
513 				   IEEE80211_PRIVACY_ANY,
514 				   NL80211_BSS_USE_FOR_NORMAL, &extack);
515 	KUNIT_EXPECT_NULL(test, found);
516 	KUNIT_EXPECT_STREQ(test, extack._msg,
517 			   "BSS entries are expired or cannot be used for the requested operation");
518 
519 	cfg80211_put_bss(wiphy, bss2);
520 	cfg80211_put_bss(wiphy, bss);
521 }
522 
523 static struct inform_bss_ml_sta_case {
524 	const char *desc;
525 	int mld_id;
526 	bool sta_prof_vendor_elems;
527 	bool include_oper_class;
528 	bool nstr;
529 } inform_bss_ml_sta_cases[] = {
530 	{
531 		.desc = "zero_mld_id",
532 		.mld_id = 0,
533 		.sta_prof_vendor_elems = false,
534 	}, {
535 		.desc = "zero_mld_id_with_oper_class",
536 		.mld_id = 0,
537 		.sta_prof_vendor_elems = false,
538 		.include_oper_class = true,
539 	}, {
540 		.desc = "mld_id_eq_1",
541 		.mld_id = 1,
542 		.sta_prof_vendor_elems = true,
543 	}, {
544 		.desc = "mld_id_eq_1_with_oper_class",
545 		.mld_id = 1,
546 		.sta_prof_vendor_elems = true,
547 		.include_oper_class = true,
548 	}, {
549 		.desc = "nstr",
550 		.mld_id = 0,
551 		.nstr = true,
552 	},
553 };
KUNIT_ARRAY_PARAM_DESC(inform_bss_ml_sta,inform_bss_ml_sta_cases,desc)554 KUNIT_ARRAY_PARAM_DESC(inform_bss_ml_sta, inform_bss_ml_sta_cases, desc)
555 
556 static void test_inform_bss_ml_sta(struct kunit *test)
557 {
558 	const struct inform_bss_ml_sta_case *params = test->param_value;
559 	struct inform_bss ctx = {
560 		.test = test,
561 	};
562 	struct wiphy *wiphy = T_WIPHY(test, ctx);
563 	struct t_wiphy_priv *w_priv = wiphy_priv(wiphy);
564 	struct cfg80211_inform_bss inform_bss = {
565 		.signal = 50,
566 		.drv_data = &ctx,
567 	};
568 	struct cfg80211_bss *bss, *link_bss;
569 	const struct cfg80211_bss_ies *ies;
570 
571 	/* sending station */
572 	const u8 bssid[ETH_ALEN] = { 0x10, 0x22, 0x33, 0x44, 0x55, 0x66 };
573 	u64 tsf = 0x1000000000000000ULL;
574 	int beacon_int = 100;
575 	u16 capability = 0x1234;
576 
577 	/* Building the frame *************************************************/
578 	struct sk_buff *input = kunit_zalloc_skb(test, 1024, GFP_KERNEL);
579 	u8 *len_mle, *len_prof;
580 	u8 link_id = 2;
581 	struct {
582 		struct ieee80211_neighbor_ap_info info;
583 		struct ieee80211_tbtt_info_ge_11 ap;
584 	} __packed rnr_normal = {
585 		.info = {
586 			.tbtt_info_hdr = u8_encode_bits(0, IEEE80211_AP_INFO_TBTT_HDR_COUNT),
587 			.tbtt_info_len = sizeof(struct ieee80211_tbtt_info_ge_11),
588 			.op_class = 81,
589 			.channel = 11,
590 		},
591 		.ap = {
592 			.tbtt_offset = 0xff,
593 			.bssid = { 0x10, 0x22, 0x33, 0x44, 0x55, 0x67 },
594 			.short_ssid = 0, /* unused */
595 			.bss_params = 0,
596 			.psd_20 = 0,
597 			.mld_params.mld_id = params->mld_id,
598 			.mld_params.params =
599 				le16_encode_bits(link_id,
600 						 IEEE80211_RNR_MLD_PARAMS_LINK_ID),
601 		}
602 	};
603 	struct {
604 		struct ieee80211_neighbor_ap_info info;
605 		struct ieee80211_rnr_mld_params mld_params;
606 	} __packed rnr_nstr = {
607 		.info = {
608 			.tbtt_info_hdr =
609 				u8_encode_bits(0, IEEE80211_AP_INFO_TBTT_HDR_COUNT) |
610 				u8_encode_bits(IEEE80211_TBTT_INFO_TYPE_MLD,
611 					       IEEE80211_AP_INFO_TBTT_HDR_TYPE),
612 			.tbtt_info_len = sizeof(struct ieee80211_rnr_mld_params),
613 			.op_class = 81,
614 			.channel = 11,
615 		},
616 		.mld_params = {
617 			.mld_id = params->mld_id,
618 			.params =
619 				le16_encode_bits(link_id,
620 						 IEEE80211_RNR_MLD_PARAMS_LINK_ID),
621 		}
622 	};
623 	size_t rnr_len = params->nstr ? sizeof(rnr_nstr) : sizeof(rnr_normal);
624 	void *rnr = params->nstr ? (void *)&rnr_nstr : (void *)&rnr_normal;
625 	struct {
626 		__le16 control;
627 		u8 var_len;
628 		u8 mld_mac_addr[ETH_ALEN];
629 		u8 link_id_info;
630 		u8 params_change_count;
631 		__le16 mld_caps_and_ops;
632 		u8 mld_id;
633 		__le16 ext_mld_caps_and_ops;
634 	} __packed mle_basic_common_info = {
635 		.control =
636 			cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC |
637 				    IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT |
638 				    IEEE80211_MLC_BASIC_PRES_LINK_ID |
639 				    (params->mld_id ? IEEE80211_MLC_BASIC_PRES_MLD_ID : 0) |
640 				    IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP),
641 		.mld_id = params->mld_id,
642 		.mld_caps_and_ops = cpu_to_le16(0x0102),
643 		.ext_mld_caps_and_ops = cpu_to_le16(0x0304),
644 		.var_len = sizeof(mle_basic_common_info) - 2 -
645 			   (params->mld_id ? 0 : 1),
646 		.mld_mac_addr = { 0x10, 0x22, 0x33, 0x44, 0x55, 0x60 },
647 	};
648 	struct {
649 		__le16 control;
650 		u8 var_len;
651 		u8 bssid[ETH_ALEN];
652 		__le16 beacon_int;
653 		__le64 tsf_offset;
654 		__le16 capabilities; /* already part of payload */
655 	} __packed sta_prof = {
656 		.control =
657 			cpu_to_le16(IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE |
658 				    IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT |
659 				    IEEE80211_MLE_STA_CONTROL_BEACON_INT_PRESENT |
660 				    IEEE80211_MLE_STA_CONTROL_TSF_OFFS_PRESENT |
661 				    u16_encode_bits(link_id,
662 						    IEEE80211_MLE_STA_CONTROL_LINK_ID)),
663 		.var_len = sizeof(sta_prof) - 2 - 2,
664 		.bssid = { *rnr_normal.ap.bssid },
665 		.beacon_int = cpu_to_le16(101),
666 		.tsf_offset = cpu_to_le64(-123ll),
667 		.capabilities = cpu_to_le16(0xdead),
668 	};
669 
670 	KUNIT_ASSERT_NOT_NULL(test, input);
671 
672 	w_priv->ops->inform_bss = inform_bss_inc_counter;
673 
674 	inform_bss.chan = ieee80211_get_channel_khz(wiphy, MHZ_TO_KHZ(2412));
675 	KUNIT_ASSERT_NOT_NULL(test, inform_bss.chan);
676 
677 	skb_put_u8(input, WLAN_EID_SSID);
678 	skb_put_u8(input, 4);
679 	skb_put_data(input, "TEST", 4);
680 
681 	if (params->include_oper_class) {
682 		skb_put_u8(input, WLAN_EID_SUPPORTED_REGULATORY_CLASSES);
683 		skb_put_u8(input, 1);
684 		skb_put_u8(input, 81);
685 	}
686 
687 	skb_put_u8(input, WLAN_EID_REDUCED_NEIGHBOR_REPORT);
688 	skb_put_u8(input, rnr_len);
689 	skb_put_data(input, rnr, rnr_len);
690 
691 	/* build a multi-link element */
692 	skb_put_u8(input, WLAN_EID_EXTENSION);
693 	len_mle = skb_put(input, 1);
694 	skb_put_u8(input, WLAN_EID_EXT_EHT_MULTI_LINK);
695 	skb_put_data(input, &mle_basic_common_info, sizeof(mle_basic_common_info));
696 	if (!params->mld_id)
697 		t_skb_remove_member(input, typeof(mle_basic_common_info), mld_id);
698 	/* with a STA profile inside */
699 	skb_put_u8(input, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE);
700 	len_prof = skb_put(input, 1);
701 	skb_put_data(input, &sta_prof, sizeof(sta_prof));
702 
703 	if (params->sta_prof_vendor_elems) {
704 		/* Put two (vendor) element into sta_prof */
705 		skb_put_u8(input, WLAN_EID_VENDOR_SPECIFIC);
706 		skb_put_u8(input, 160);
707 		skb_put(input, 160);
708 
709 		skb_put_u8(input, WLAN_EID_VENDOR_SPECIFIC);
710 		skb_put_u8(input, 165);
711 		skb_put(input, 165);
712 	}
713 
714 	/* fragment STA profile */
715 	ieee80211_fragment_element(input, len_prof,
716 				   IEEE80211_MLE_SUBELEM_FRAGMENT);
717 	/* fragment MLE */
718 	ieee80211_fragment_element(input, len_mle, WLAN_EID_FRAGMENT);
719 
720 	/* Put a (vendor) element after the ML element */
721 	skb_put_u8(input, WLAN_EID_VENDOR_SPECIFIC);
722 	skb_put_u8(input, 155);
723 	skb_put(input, 155);
724 
725 	/* Submit *************************************************************/
726 	bss = cfg80211_inform_bss_data(wiphy, &inform_bss,
727 				       CFG80211_BSS_FTYPE_PRESP, bssid, tsf,
728 				       capability, beacon_int,
729 				       input->data, input->len,
730 				       GFP_KERNEL);
731 	KUNIT_EXPECT_NOT_NULL(test, bss);
732 	KUNIT_EXPECT_EQ(test, ctx.inform_bss_count, 2);
733 
734 	/* Check link_bss *****************************************************/
735 	link_bss = __cfg80211_get_bss(wiphy, NULL, sta_prof.bssid, NULL, 0,
736 				      IEEE80211_BSS_TYPE_ANY,
737 				      IEEE80211_PRIVACY_ANY,
738 				      0, NULL);
739 	KUNIT_ASSERT_NOT_NULL(test, link_bss);
740 	KUNIT_EXPECT_EQ(test, link_bss->signal, 0);
741 	KUNIT_EXPECT_EQ(test, link_bss->beacon_interval,
742 			      le16_to_cpu(sta_prof.beacon_int));
743 	KUNIT_EXPECT_EQ(test, link_bss->capability,
744 			      le16_to_cpu(sta_prof.capabilities));
745 	KUNIT_EXPECT_EQ(test, link_bss->bssid_index, 0);
746 	KUNIT_EXPECT_PTR_EQ(test, link_bss->channel,
747 			    ieee80211_get_channel_khz(wiphy, MHZ_TO_KHZ(2462)));
748 
749 	/* Test wiphy does not set WIPHY_FLAG_SUPPORTS_NSTR_NONPRIMARY */
750 	if (params->nstr) {
751 		KUNIT_EXPECT_EQ(test, link_bss->use_for, 0);
752 		KUNIT_EXPECT_EQ(test, link_bss->cannot_use_reasons,
753 				NL80211_BSS_CANNOT_USE_NSTR_NONPRIMARY);
754 		KUNIT_EXPECT_NULL(test,
755 				  cfg80211_get_bss(wiphy, NULL, sta_prof.bssid,
756 						   NULL, 0,
757 						   IEEE80211_BSS_TYPE_ANY,
758 						   IEEE80211_PRIVACY_ANY));
759 	} else {
760 		KUNIT_EXPECT_EQ(test, link_bss->use_for,
761 				NL80211_BSS_USE_FOR_ALL);
762 		KUNIT_EXPECT_EQ(test, link_bss->cannot_use_reasons, 0);
763 	}
764 
765 	rcu_read_lock();
766 	ies = rcu_dereference(link_bss->ies);
767 	KUNIT_EXPECT_NOT_NULL(test, ies);
768 	KUNIT_EXPECT_EQ(test, ies->tsf, tsf + le64_to_cpu(sta_prof.tsf_offset));
769 	/* Resulting length should be:
770 	 * SSID (inherited) + RNR (inherited) + vendor element(s) +
771 	 * operating class (if requested) +
772 	 * generated RNR (if MLD ID == 0 and not NSTR) +
773 	 * MLE common info + MLE header and control
774 	 */
775 	if (params->sta_prof_vendor_elems)
776 		KUNIT_EXPECT_EQ(test, ies->len,
777 				6 + 2 + rnr_len + 2 + 160 + 2 + 165 +
778 				(params->include_oper_class ? 3 : 0) +
779 				(!params->mld_id && !params->nstr ? 22 : 0) +
780 				mle_basic_common_info.var_len + 5);
781 	else
782 		KUNIT_EXPECT_EQ(test, ies->len,
783 				6 + 2 + rnr_len + 2 + 155 +
784 				(params->include_oper_class ? 3 : 0) +
785 				(!params->mld_id && !params->nstr ? 22 : 0) +
786 				mle_basic_common_info.var_len + 5);
787 	rcu_read_unlock();
788 
789 	cfg80211_put_bss(wiphy, bss);
790 	cfg80211_put_bss(wiphy, link_bss);
791 }
792 
793 static struct cfg80211_parse_colocated_ap_case {
794 	const char *desc;
795 	u8 op_class;
796 	u8 channel;
797 	struct ieee80211_neighbor_ap_info info;
798 	union {
799 		struct ieee80211_tbtt_info_ge_11 tbtt_long;
800 		struct ieee80211_tbtt_info_7_8_9 tbtt_short;
801 	};
802 	bool add_junk;
803 	bool same_ssid;
804 	bool valid;
805 } cfg80211_parse_colocated_ap_cases[] = {
806 	{
807 		.desc = "wrong_band",
808 		.info.op_class = 81,
809 		.info.channel = 11,
810 		.tbtt_long = {
811 			.bssid = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
812 			.bss_params = IEEE80211_RNR_TBTT_PARAMS_COLOC_AP,
813 		},
814 		.valid = false,
815 	},
816 	{
817 		.desc = "wrong_type",
818 		/* IEEE80211_AP_INFO_TBTT_HDR_TYPE is in the least significant bits */
819 		.info.tbtt_info_hdr = IEEE80211_TBTT_INFO_TYPE_MLD,
820 		.tbtt_long = {
821 			.bssid = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
822 			.bss_params = IEEE80211_RNR_TBTT_PARAMS_COLOC_AP,
823 		},
824 		.valid = false,
825 	},
826 	{
827 		.desc = "colocated_invalid_len_short",
828 		.info.tbtt_info_len = 6,
829 		.tbtt_short = {
830 			.bssid = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
831 			.bss_params = IEEE80211_RNR_TBTT_PARAMS_COLOC_AP |
832 				      IEEE80211_RNR_TBTT_PARAMS_SAME_SSID,
833 		},
834 		.valid = false,
835 	},
836 	{
837 		.desc = "colocated_invalid_len_short_mld",
838 		.info.tbtt_info_len = 10,
839 		.tbtt_long = {
840 			.bssid = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
841 			.bss_params = IEEE80211_RNR_TBTT_PARAMS_COLOC_AP,
842 		},
843 		.valid = false,
844 	},
845 	{
846 		.desc = "colocated_non_mld",
847 		.info.tbtt_info_len = sizeof(struct ieee80211_tbtt_info_7_8_9),
848 		.tbtt_short = {
849 			.bssid = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
850 			.bss_params = IEEE80211_RNR_TBTT_PARAMS_COLOC_AP |
851 				      IEEE80211_RNR_TBTT_PARAMS_SAME_SSID,
852 		},
853 		.same_ssid = true,
854 		.valid = true,
855 	},
856 	{
857 		.desc = "colocated_non_mld_invalid_bssid",
858 		.info.tbtt_info_len = sizeof(struct ieee80211_tbtt_info_7_8_9),
859 		.tbtt_short = {
860 			.bssid = { 0xff, 0x11, 0x22, 0x33, 0x44, 0x55 },
861 			.bss_params = IEEE80211_RNR_TBTT_PARAMS_COLOC_AP |
862 				      IEEE80211_RNR_TBTT_PARAMS_SAME_SSID,
863 		},
864 		.same_ssid = true,
865 		.valid = false,
866 	},
867 	{
868 		.desc = "colocated_mld",
869 		.tbtt_long = {
870 			.bssid = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
871 			.bss_params = IEEE80211_RNR_TBTT_PARAMS_COLOC_AP,
872 		},
873 		.valid = true,
874 	},
875 	{
876 		.desc = "colocated_mld",
877 		.tbtt_long = {
878 			.bssid = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
879 			.bss_params = IEEE80211_RNR_TBTT_PARAMS_COLOC_AP,
880 		},
881 		.add_junk = true,
882 		.valid = false,
883 	},
884 	{
885 		.desc = "colocated_disabled_mld",
886 		.tbtt_long = {
887 			.bssid = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 },
888 			.bss_params = IEEE80211_RNR_TBTT_PARAMS_COLOC_AP,
889 			.mld_params.params = cpu_to_le16(IEEE80211_RNR_MLD_PARAMS_DISABLED_LINK),
890 		},
891 		.valid = false,
892 	},
893 };
KUNIT_ARRAY_PARAM_DESC(cfg80211_parse_colocated_ap,cfg80211_parse_colocated_ap_cases,desc)894 KUNIT_ARRAY_PARAM_DESC(cfg80211_parse_colocated_ap, cfg80211_parse_colocated_ap_cases, desc)
895 
896 static void test_cfg80211_parse_colocated_ap(struct kunit *test)
897 {
898 	const struct cfg80211_parse_colocated_ap_case *params = test->param_value;
899 	struct sk_buff *input = kunit_zalloc_skb(test, 1024, GFP_KERNEL);
900 	struct cfg80211_bss_ies *ies;
901 	struct ieee80211_neighbor_ap_info info;
902 	LIST_HEAD(coloc_ap_list);
903 	int count;
904 
905 	KUNIT_ASSERT_NOT_NULL(test, input);
906 
907 	info = params->info;
908 
909 	/* Reasonable values for a colocated AP */
910 	if (!info.tbtt_info_len)
911 		info.tbtt_info_len = sizeof(params->tbtt_long);
912 	if (!info.op_class)
913 		info.op_class = 131;
914 	if (!info.channel)
915 		info.channel = 33;
916 	/* Zero is the correct default for .btt_info_hdr (one entry, TBTT type) */
917 
918 	skb_put_u8(input, WLAN_EID_SSID);
919 	skb_put_u8(input, 4);
920 	skb_put_data(input, "TEST", 4);
921 
922 	skb_put_u8(input, WLAN_EID_REDUCED_NEIGHBOR_REPORT);
923 	skb_put_u8(input, sizeof(info) + info.tbtt_info_len + (params->add_junk ? 3 : 0));
924 	skb_put_data(input, &info, sizeof(info));
925 	skb_put_data(input, &params->tbtt_long, info.tbtt_info_len);
926 
927 	if (params->add_junk)
928 		skb_put_data(input, "123", 3);
929 
930 	ies = kunit_kzalloc(test, struct_size(ies, data, input->len), GFP_KERNEL);
931 	KUNIT_ASSERT_NOT_NULL(test, ies);
932 
933 	ies->len = input->len;
934 	memcpy(ies->data, input->data, input->len);
935 
936 	count = cfg80211_parse_colocated_ap(ies, &coloc_ap_list);
937 
938 	KUNIT_EXPECT_EQ(test, count, params->valid);
939 	KUNIT_EXPECT_EQ(test, list_count_nodes(&coloc_ap_list), params->valid);
940 
941 	if (params->valid && !list_empty(&coloc_ap_list)) {
942 		struct cfg80211_colocated_ap *ap;
943 
944 		ap = list_first_entry(&coloc_ap_list, typeof(*ap), list);
945 		if (info.tbtt_info_len <= sizeof(params->tbtt_short))
946 			KUNIT_EXPECT_MEMEQ(test, ap->bssid, params->tbtt_short.bssid, ETH_ALEN);
947 		else
948 			KUNIT_EXPECT_MEMEQ(test, ap->bssid, params->tbtt_long.bssid, ETH_ALEN);
949 
950 		if (params->same_ssid) {
951 			KUNIT_EXPECT_EQ(test, ap->ssid_len, 4);
952 			KUNIT_EXPECT_MEMEQ(test, ap->ssid, "TEST", 4);
953 		} else {
954 			KUNIT_EXPECT_EQ(test, ap->ssid_len, 0);
955 		}
956 	}
957 
958 	cfg80211_free_coloc_ap_list(&coloc_ap_list);
959 }
960 
961 static struct kunit_case gen_new_ie_test_cases[] = {
962 	KUNIT_CASE_PARAM(test_gen_new_ie, gen_new_ie_gen_params),
963 	KUNIT_CASE(test_gen_new_ie_malformed),
964 	{}
965 };
966 
967 static struct kunit_suite gen_new_ie = {
968 	.name = "cfg80211-ie-generation",
969 	.test_cases = gen_new_ie_test_cases,
970 };
971 
972 kunit_test_suite(gen_new_ie);
973 
974 static struct kunit_case inform_bss_test_cases[] = {
975 	KUNIT_CASE(test_inform_bss_ssid_only),
976 	KUNIT_CASE(test_get_bss_miss_reason),
977 	KUNIT_CASE_PARAM(test_inform_bss_ml_sta, inform_bss_ml_sta_gen_params),
978 	{}
979 };
980 
981 static struct kunit_suite inform_bss = {
982 	.name = "cfg80211-inform-bss",
983 	.test_cases = inform_bss_test_cases,
984 };
985 
986 kunit_test_suite(inform_bss);
987 
988 static struct kunit_case scan_6ghz_cases[] = {
989 	KUNIT_CASE_PARAM(test_cfg80211_parse_colocated_ap,
990 			 cfg80211_parse_colocated_ap_gen_params),
991 	{}
992 };
993 
994 static struct kunit_suite scan_6ghz = {
995 	.name = "cfg80211-scan-6ghz",
996 	.test_cases = scan_6ghz_cases,
997 };
998 
999 kunit_test_suite(scan_6ghz);
1000