xref: /openbmc/linux/net/mac80211/debugfs_netdev.c (revision f4356947)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2006	Jiri Benc <jbenc@suse.cz>
4  * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
5  * Copyright (C) 2020-2022 Intel Corporation
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/device.h>
10 #include <linux/if.h>
11 #include <linux/if_ether.h>
12 #include <linux/interrupt.h>
13 #include <linux/netdevice.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/slab.h>
16 #include <linux/notifier.h>
17 #include <net/mac80211.h>
18 #include <net/cfg80211.h>
19 #include "ieee80211_i.h"
20 #include "rate.h"
21 #include "debugfs.h"
22 #include "debugfs_netdev.h"
23 #include "driver-ops.h"
24 
25 static ssize_t ieee80211_if_read(
26 	void *data,
27 	char __user *userbuf,
28 	size_t count, loff_t *ppos,
29 	ssize_t (*format)(const void *, char *, int))
30 {
31 	char buf[200];
32 	ssize_t ret = -EINVAL;
33 
34 	read_lock(&dev_base_lock);
35 	ret = (*format)(data, buf, sizeof(buf));
36 	read_unlock(&dev_base_lock);
37 
38 	if (ret >= 0)
39 		ret = simple_read_from_buffer(userbuf, count, ppos, buf, ret);
40 
41 	return ret;
42 }
43 
44 static ssize_t ieee80211_if_write(
45 	void *data,
46 	const char __user *userbuf,
47 	size_t count, loff_t *ppos,
48 	ssize_t (*write)(void *, const char *, int))
49 {
50 	char buf[64];
51 	ssize_t ret;
52 
53 	if (count >= sizeof(buf))
54 		return -E2BIG;
55 
56 	if (copy_from_user(buf, userbuf, count))
57 		return -EFAULT;
58 	buf[count] = '\0';
59 
60 	rtnl_lock();
61 	ret = (*write)(data, buf, count);
62 	rtnl_unlock();
63 
64 	return ret;
65 }
66 
67 #define IEEE80211_IF_FMT(name, type, field, format_string)		\
68 static ssize_t ieee80211_if_fmt_##name(					\
69 	const type *data, char *buf,					\
70 	int buflen)							\
71 {									\
72 	return scnprintf(buf, buflen, format_string, data->field);	\
73 }
74 #define IEEE80211_IF_FMT_DEC(name, type, field)				\
75 		IEEE80211_IF_FMT(name, type, field, "%d\n")
76 #define IEEE80211_IF_FMT_HEX(name, type, field)				\
77 		IEEE80211_IF_FMT(name, type, field, "%#x\n")
78 #define IEEE80211_IF_FMT_LHEX(name, type, field)			\
79 		IEEE80211_IF_FMT(name, type, field, "%#lx\n")
80 
81 #define IEEE80211_IF_FMT_HEXARRAY(name, type, field)			\
82 static ssize_t ieee80211_if_fmt_##name(					\
83 	const type *data,						\
84 	char *buf, int buflen)						\
85 {									\
86 	char *p = buf;							\
87 	int i;								\
88 	for (i = 0; i < sizeof(data->field); i++) {			\
89 		p += scnprintf(p, buflen + buf - p, "%.2x ",		\
90 				 data->field[i]);			\
91 	}								\
92 	p += scnprintf(p, buflen + buf - p, "\n");			\
93 	return p - buf;							\
94 }
95 
96 #define IEEE80211_IF_FMT_ATOMIC(name, type, field)			\
97 static ssize_t ieee80211_if_fmt_##name(					\
98 	const type *data,						\
99 	char *buf, int buflen)						\
100 {									\
101 	return scnprintf(buf, buflen, "%d\n", atomic_read(&data->field));\
102 }
103 
104 #define IEEE80211_IF_FMT_MAC(name, type, field)				\
105 static ssize_t ieee80211_if_fmt_##name(					\
106 	const type *data, char *buf,					\
107 	int buflen)							\
108 {									\
109 	return scnprintf(buf, buflen, "%pM\n", data->field);		\
110 }
111 
112 #define IEEE80211_IF_FMT_JIFFIES_TO_MS(name, type, field)		\
113 static ssize_t ieee80211_if_fmt_##name(					\
114 	const type *data,						\
115 	char *buf, int buflen)						\
116 {									\
117 	return scnprintf(buf, buflen, "%d\n",				\
118 			 jiffies_to_msecs(data->field));		\
119 }
120 
121 #define _IEEE80211_IF_FILE_OPS(name, _read, _write)			\
122 static const struct file_operations name##_ops = {			\
123 	.read = (_read),						\
124 	.write = (_write),						\
125 	.open = simple_open,						\
126 	.llseek = generic_file_llseek,					\
127 }
128 
129 #define _IEEE80211_IF_FILE_R_FN(name, type)				\
130 static ssize_t ieee80211_if_read_##name(struct file *file,		\
131 					char __user *userbuf,		\
132 					size_t count, loff_t *ppos)	\
133 {									\
134 	ssize_t (*fn)(const void *, char *, int) = (void *)		\
135 		((ssize_t (*)(const type, char *, int))			\
136 		 ieee80211_if_fmt_##name);				\
137 	return ieee80211_if_read(file->private_data,			\
138 				 userbuf, count, ppos, fn);		\
139 }
140 
141 #define _IEEE80211_IF_FILE_W_FN(name, type)				\
142 static ssize_t ieee80211_if_write_##name(struct file *file,		\
143 					 const char __user *userbuf,	\
144 					 size_t count, loff_t *ppos)	\
145 {									\
146 	ssize_t (*fn)(void *, const char *, int) = (void *)		\
147 		((ssize_t (*)(type, const char *, int))			\
148 		 ieee80211_if_parse_##name);				\
149 	return ieee80211_if_write(file->private_data, userbuf, count,	\
150 				  ppos, fn);				\
151 }
152 
153 #define IEEE80211_IF_FILE_R(name)					\
154 	_IEEE80211_IF_FILE_R_FN(name, struct ieee80211_sub_if_data *)	\
155 	_IEEE80211_IF_FILE_OPS(name, ieee80211_if_read_##name, NULL)
156 
157 #define IEEE80211_IF_FILE_W(name)					\
158 	_IEEE80211_IF_FILE_W_FN(name, struct ieee80211_sub_if_data *)	\
159 	_IEEE80211_IF_FILE_OPS(name, NULL, ieee80211_if_write_##name)
160 
161 #define IEEE80211_IF_FILE_RW(name)					\
162 	_IEEE80211_IF_FILE_R_FN(name, struct ieee80211_sub_if_data *)	\
163 	_IEEE80211_IF_FILE_W_FN(name, struct ieee80211_sub_if_data *)	\
164 	_IEEE80211_IF_FILE_OPS(name, ieee80211_if_read_##name,		\
165 			       ieee80211_if_write_##name)
166 
167 #define IEEE80211_IF_FILE(name, field, format)				\
168 	IEEE80211_IF_FMT_##format(name, struct ieee80211_sub_if_data, field) \
169 	IEEE80211_IF_FILE_R(name)
170 
171 /* Same but with a link_ prefix in the ops variable name and different type */
172 #define IEEE80211_IF_LINK_FILE_R(name)					\
173 	_IEEE80211_IF_FILE_R_FN(name, struct ieee80211_link_data *)	\
174 	_IEEE80211_IF_FILE_OPS(link_##name, ieee80211_if_read_##name, NULL)
175 
176 #define IEEE80211_IF_LINK_FILE_W(name)					\
177 	_IEEE80211_IF_FILE_W_FN(name)					\
178 	_IEEE80211_IF_FILE_OPS(link_##name, NULL, ieee80211_if_write_##name)
179 
180 #define IEEE80211_IF_LINK_FILE_RW(name)					\
181 	_IEEE80211_IF_FILE_R_FN(name, struct ieee80211_link_data *)	\
182 	_IEEE80211_IF_FILE_W_FN(name, struct ieee80211_link_data *)	\
183 	_IEEE80211_IF_FILE_OPS(link_##name, ieee80211_if_read_##name,	\
184 			       ieee80211_if_write_##name)
185 
186 #define IEEE80211_IF_LINK_FILE(name, field, format)				\
187 	IEEE80211_IF_FMT_##format(name, struct ieee80211_link_data, field) \
188 	IEEE80211_IF_LINK_FILE_R(name)
189 
190 /* common attributes */
191 IEEE80211_IF_FILE(rc_rateidx_mask_2ghz, rc_rateidx_mask[NL80211_BAND_2GHZ],
192 		  HEX);
193 IEEE80211_IF_FILE(rc_rateidx_mask_5ghz, rc_rateidx_mask[NL80211_BAND_5GHZ],
194 		  HEX);
195 IEEE80211_IF_FILE(rc_rateidx_mcs_mask_2ghz,
196 		  rc_rateidx_mcs_mask[NL80211_BAND_2GHZ], HEXARRAY);
197 IEEE80211_IF_FILE(rc_rateidx_mcs_mask_5ghz,
198 		  rc_rateidx_mcs_mask[NL80211_BAND_5GHZ], HEXARRAY);
199 
200 static ssize_t ieee80211_if_fmt_rc_rateidx_vht_mcs_mask_2ghz(
201 				const struct ieee80211_sub_if_data *sdata,
202 				char *buf, int buflen)
203 {
204 	int i, len = 0;
205 	const u16 *mask = sdata->rc_rateidx_vht_mcs_mask[NL80211_BAND_2GHZ];
206 
207 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
208 		len += scnprintf(buf + len, buflen - len, "%04x ", mask[i]);
209 	len += scnprintf(buf + len, buflen - len, "\n");
210 
211 	return len;
212 }
213 
214 IEEE80211_IF_FILE_R(rc_rateidx_vht_mcs_mask_2ghz);
215 
216 static ssize_t ieee80211_if_fmt_rc_rateidx_vht_mcs_mask_5ghz(
217 				const struct ieee80211_sub_if_data *sdata,
218 				char *buf, int buflen)
219 {
220 	int i, len = 0;
221 	const u16 *mask = sdata->rc_rateidx_vht_mcs_mask[NL80211_BAND_5GHZ];
222 
223 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
224 		len += scnprintf(buf + len, buflen - len, "%04x ", mask[i]);
225 	len += scnprintf(buf + len, buflen - len, "\n");
226 
227 	return len;
228 }
229 
230 IEEE80211_IF_FILE_R(rc_rateidx_vht_mcs_mask_5ghz);
231 
232 IEEE80211_IF_FILE(flags, flags, HEX);
233 IEEE80211_IF_FILE(state, state, LHEX);
234 IEEE80211_IF_LINK_FILE(txpower, conf->txpower, DEC);
235 IEEE80211_IF_LINK_FILE(ap_power_level, ap_power_level, DEC);
236 IEEE80211_IF_LINK_FILE(user_power_level, user_power_level, DEC);
237 
238 static ssize_t
239 ieee80211_if_fmt_hw_queues(const struct ieee80211_sub_if_data *sdata,
240 			   char *buf, int buflen)
241 {
242 	int len;
243 
244 	len = scnprintf(buf, buflen, "AC queues: VO:%d VI:%d BE:%d BK:%d\n",
245 			sdata->vif.hw_queue[IEEE80211_AC_VO],
246 			sdata->vif.hw_queue[IEEE80211_AC_VI],
247 			sdata->vif.hw_queue[IEEE80211_AC_BE],
248 			sdata->vif.hw_queue[IEEE80211_AC_BK]);
249 
250 	if (sdata->vif.type == NL80211_IFTYPE_AP)
251 		len += scnprintf(buf + len, buflen - len, "cab queue: %d\n",
252 				 sdata->vif.cab_queue);
253 
254 	return len;
255 }
256 IEEE80211_IF_FILE_R(hw_queues);
257 
258 /* STA attributes */
259 IEEE80211_IF_FILE(bssid, deflink.u.mgd.bssid, MAC);
260 IEEE80211_IF_FILE(aid, vif.cfg.aid, DEC);
261 IEEE80211_IF_FILE(beacon_timeout, u.mgd.beacon_timeout, JIFFIES_TO_MS);
262 
263 static int ieee80211_set_smps(struct ieee80211_link_data *link,
264 			      enum ieee80211_smps_mode smps_mode)
265 {
266 	struct ieee80211_sub_if_data *sdata = link->sdata;
267 	struct ieee80211_local *local = sdata->local;
268 	int err;
269 
270 	if (!(local->hw.wiphy->features & NL80211_FEATURE_STATIC_SMPS) &&
271 	    smps_mode == IEEE80211_SMPS_STATIC)
272 		return -EINVAL;
273 
274 	/* auto should be dynamic if in PS mode */
275 	if (!(local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS) &&
276 	    (smps_mode == IEEE80211_SMPS_DYNAMIC ||
277 	     smps_mode == IEEE80211_SMPS_AUTOMATIC))
278 		return -EINVAL;
279 
280 	if (sdata->vif.type != NL80211_IFTYPE_STATION)
281 		return -EOPNOTSUPP;
282 
283 	sdata_lock(sdata);
284 	err = __ieee80211_request_smps_mgd(link->sdata, link, smps_mode);
285 	sdata_unlock(sdata);
286 
287 	return err;
288 }
289 
290 static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
291 	[IEEE80211_SMPS_AUTOMATIC] = "auto",
292 	[IEEE80211_SMPS_OFF] = "off",
293 	[IEEE80211_SMPS_STATIC] = "static",
294 	[IEEE80211_SMPS_DYNAMIC] = "dynamic",
295 };
296 
297 static ssize_t ieee80211_if_fmt_smps(const struct ieee80211_link_data *link,
298 				     char *buf, int buflen)
299 {
300 	if (link->sdata->vif.type == NL80211_IFTYPE_STATION)
301 		return snprintf(buf, buflen, "request: %s\nused: %s\n",
302 				smps_modes[link->u.mgd.req_smps],
303 				smps_modes[link->smps_mode]);
304 	return -EINVAL;
305 }
306 
307 static ssize_t ieee80211_if_parse_smps(struct ieee80211_link_data *link,
308 				       const char *buf, int buflen)
309 {
310 	enum ieee80211_smps_mode mode;
311 
312 	for (mode = 0; mode < IEEE80211_SMPS_NUM_MODES; mode++) {
313 		if (strncmp(buf, smps_modes[mode], buflen) == 0) {
314 			int err = ieee80211_set_smps(link, mode);
315 			if (!err)
316 				return buflen;
317 			return err;
318 		}
319 	}
320 
321 	return -EINVAL;
322 }
323 IEEE80211_IF_LINK_FILE_RW(smps);
324 
325 static ssize_t ieee80211_if_parse_tkip_mic_test(
326 	struct ieee80211_sub_if_data *sdata, const char *buf, int buflen)
327 {
328 	struct ieee80211_local *local = sdata->local;
329 	u8 addr[ETH_ALEN];
330 	struct sk_buff *skb;
331 	struct ieee80211_hdr *hdr;
332 	__le16 fc;
333 
334 	if (!mac_pton(buf, addr))
335 		return -EINVAL;
336 
337 	if (!ieee80211_sdata_running(sdata))
338 		return -ENOTCONN;
339 
340 	skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24 + 100);
341 	if (!skb)
342 		return -ENOMEM;
343 	skb_reserve(skb, local->hw.extra_tx_headroom);
344 
345 	hdr = skb_put_zero(skb, 24);
346 	fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
347 
348 	switch (sdata->vif.type) {
349 	case NL80211_IFTYPE_AP:
350 		fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
351 		/* DA BSSID SA */
352 		memcpy(hdr->addr1, addr, ETH_ALEN);
353 		memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
354 		memcpy(hdr->addr3, sdata->vif.addr, ETH_ALEN);
355 		break;
356 	case NL80211_IFTYPE_STATION:
357 		fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
358 		/* BSSID SA DA */
359 		sdata_lock(sdata);
360 		if (!sdata->u.mgd.associated) {
361 			sdata_unlock(sdata);
362 			dev_kfree_skb(skb);
363 			return -ENOTCONN;
364 		}
365 		memcpy(hdr->addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN);
366 		memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
367 		memcpy(hdr->addr3, addr, ETH_ALEN);
368 		sdata_unlock(sdata);
369 		break;
370 	default:
371 		dev_kfree_skb(skb);
372 		return -EOPNOTSUPP;
373 	}
374 	hdr->frame_control = fc;
375 
376 	/*
377 	 * Add some length to the test frame to make it look bit more valid.
378 	 * The exact contents does not matter since the recipient is required
379 	 * to drop this because of the Michael MIC failure.
380 	 */
381 	skb_put_zero(skb, 50);
382 
383 	IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_TKIP_MIC_FAILURE;
384 
385 	ieee80211_tx_skb(sdata, skb);
386 
387 	return buflen;
388 }
389 IEEE80211_IF_FILE_W(tkip_mic_test);
390 
391 static ssize_t ieee80211_if_parse_beacon_loss(
392 	struct ieee80211_sub_if_data *sdata, const char *buf, int buflen)
393 {
394 	if (!ieee80211_sdata_running(sdata) || !sdata->vif.cfg.assoc)
395 		return -ENOTCONN;
396 
397 	ieee80211_beacon_loss(&sdata->vif);
398 
399 	return buflen;
400 }
401 IEEE80211_IF_FILE_W(beacon_loss);
402 
403 static ssize_t ieee80211_if_fmt_uapsd_queues(
404 	const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
405 {
406 	const struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
407 
408 	return snprintf(buf, buflen, "0x%x\n", ifmgd->uapsd_queues);
409 }
410 
411 static ssize_t ieee80211_if_parse_uapsd_queues(
412 	struct ieee80211_sub_if_data *sdata, const char *buf, int buflen)
413 {
414 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
415 	u8 val;
416 	int ret;
417 
418 	ret = kstrtou8(buf, 0, &val);
419 	if (ret)
420 		return ret;
421 
422 	if (val & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
423 		return -ERANGE;
424 
425 	ifmgd->uapsd_queues = val;
426 
427 	return buflen;
428 }
429 IEEE80211_IF_FILE_RW(uapsd_queues);
430 
431 static ssize_t ieee80211_if_fmt_uapsd_max_sp_len(
432 	const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
433 {
434 	const struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
435 
436 	return snprintf(buf, buflen, "0x%x\n", ifmgd->uapsd_max_sp_len);
437 }
438 
439 static ssize_t ieee80211_if_parse_uapsd_max_sp_len(
440 	struct ieee80211_sub_if_data *sdata, const char *buf, int buflen)
441 {
442 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
443 	unsigned long val;
444 	int ret;
445 
446 	ret = kstrtoul(buf, 0, &val);
447 	if (ret)
448 		return -EINVAL;
449 
450 	if (val & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
451 		return -ERANGE;
452 
453 	ifmgd->uapsd_max_sp_len = val;
454 
455 	return buflen;
456 }
457 IEEE80211_IF_FILE_RW(uapsd_max_sp_len);
458 
459 static ssize_t ieee80211_if_fmt_tdls_wider_bw(
460 	const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
461 {
462 	const struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
463 	bool tdls_wider_bw;
464 
465 	tdls_wider_bw = ieee80211_hw_check(&sdata->local->hw, TDLS_WIDER_BW) &&
466 			!ifmgd->tdls_wider_bw_prohibited;
467 
468 	return snprintf(buf, buflen, "%d\n", tdls_wider_bw);
469 }
470 
471 static ssize_t ieee80211_if_parse_tdls_wider_bw(
472 	struct ieee80211_sub_if_data *sdata, const char *buf, int buflen)
473 {
474 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
475 	u8 val;
476 	int ret;
477 
478 	ret = kstrtou8(buf, 0, &val);
479 	if (ret)
480 		return ret;
481 
482 	ifmgd->tdls_wider_bw_prohibited = !val;
483 	return buflen;
484 }
485 IEEE80211_IF_FILE_RW(tdls_wider_bw);
486 
487 /* AP attributes */
488 IEEE80211_IF_FILE(num_mcast_sta, u.ap.num_mcast_sta, ATOMIC);
489 IEEE80211_IF_FILE(num_sta_ps, u.ap.ps.num_sta_ps, ATOMIC);
490 IEEE80211_IF_FILE(dtim_count, u.ap.ps.dtim_count, DEC);
491 IEEE80211_IF_FILE(num_mcast_sta_vlan, u.vlan.num_mcast_sta, ATOMIC);
492 
493 static ssize_t ieee80211_if_fmt_num_buffered_multicast(
494 	const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
495 {
496 	return scnprintf(buf, buflen, "%u\n",
497 			 skb_queue_len(&sdata->u.ap.ps.bc_buf));
498 }
499 IEEE80211_IF_FILE_R(num_buffered_multicast);
500 
501 static ssize_t ieee80211_if_fmt_aqm(
502 	const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
503 {
504 	struct ieee80211_local *local = sdata->local;
505 	struct txq_info *txqi;
506 	int len;
507 
508 	if (!sdata->vif.txq)
509 		return 0;
510 
511 	txqi = to_txq_info(sdata->vif.txq);
512 
513 	spin_lock_bh(&local->fq.lock);
514 	rcu_read_lock();
515 
516 	len = scnprintf(buf,
517 			buflen,
518 			"ac backlog-bytes backlog-packets new-flows drops marks overlimit collisions tx-bytes tx-packets\n"
519 			"%u %u %u %u %u %u %u %u %u %u\n",
520 			txqi->txq.ac,
521 			txqi->tin.backlog_bytes,
522 			txqi->tin.backlog_packets,
523 			txqi->tin.flows,
524 			txqi->cstats.drop_count,
525 			txqi->cstats.ecn_mark,
526 			txqi->tin.overlimit,
527 			txqi->tin.collisions,
528 			txqi->tin.tx_bytes,
529 			txqi->tin.tx_packets);
530 
531 	rcu_read_unlock();
532 	spin_unlock_bh(&local->fq.lock);
533 
534 	return len;
535 }
536 IEEE80211_IF_FILE_R(aqm);
537 
538 IEEE80211_IF_FILE(multicast_to_unicast, u.ap.multicast_to_unicast, HEX);
539 
540 /* IBSS attributes */
541 static ssize_t ieee80211_if_fmt_tsf(
542 	const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
543 {
544 	struct ieee80211_local *local = sdata->local;
545 	u64 tsf;
546 
547 	tsf = drv_get_tsf(local, (struct ieee80211_sub_if_data *)sdata);
548 
549 	return scnprintf(buf, buflen, "0x%016llx\n", (unsigned long long) tsf);
550 }
551 
552 static ssize_t ieee80211_if_parse_tsf(
553 	struct ieee80211_sub_if_data *sdata, const char *buf, int buflen)
554 {
555 	struct ieee80211_local *local = sdata->local;
556 	unsigned long long tsf;
557 	int ret;
558 	int tsf_is_delta = 0;
559 
560 	if (strncmp(buf, "reset", 5) == 0) {
561 		if (local->ops->reset_tsf) {
562 			drv_reset_tsf(local, sdata);
563 			wiphy_info(local->hw.wiphy, "debugfs reset TSF\n");
564 		}
565 	} else {
566 		if (buflen > 10 && buf[1] == '=') {
567 			if (buf[0] == '+')
568 				tsf_is_delta = 1;
569 			else if (buf[0] == '-')
570 				tsf_is_delta = -1;
571 			else
572 				return -EINVAL;
573 			buf += 2;
574 		}
575 		ret = kstrtoull(buf, 10, &tsf);
576 		if (ret < 0)
577 			return ret;
578 		if (tsf_is_delta && local->ops->offset_tsf) {
579 			drv_offset_tsf(local, sdata, tsf_is_delta * tsf);
580 			wiphy_info(local->hw.wiphy,
581 				   "debugfs offset TSF by %018lld\n",
582 				   tsf_is_delta * tsf);
583 		} else if (local->ops->set_tsf) {
584 			if (tsf_is_delta)
585 				tsf = drv_get_tsf(local, sdata) +
586 				      tsf_is_delta * tsf;
587 			drv_set_tsf(local, sdata, tsf);
588 			wiphy_info(local->hw.wiphy,
589 				   "debugfs set TSF to %#018llx\n", tsf);
590 		}
591 	}
592 
593 	ieee80211_recalc_dtim(local, sdata);
594 	return buflen;
595 }
596 IEEE80211_IF_FILE_RW(tsf);
597 
598 static ssize_t ieee80211_if_fmt_valid_links(const struct ieee80211_sub_if_data *sdata,
599 					    char *buf, int buflen)
600 {
601 	return snprintf(buf, buflen, "0x%x\n", sdata->vif.valid_links);
602 }
603 IEEE80211_IF_FILE_R(valid_links);
604 
605 static ssize_t ieee80211_if_fmt_active_links(const struct ieee80211_sub_if_data *sdata,
606 					     char *buf, int buflen)
607 {
608 	return snprintf(buf, buflen, "0x%x\n", sdata->vif.active_links);
609 }
610 
611 static ssize_t ieee80211_if_parse_active_links(struct ieee80211_sub_if_data *sdata,
612 					       const char *buf, int buflen)
613 {
614 	u16 active_links;
615 
616 	if (kstrtou16(buf, 0, &active_links))
617 		return -EINVAL;
618 
619 	return ieee80211_set_active_links(&sdata->vif, active_links) ?: buflen;
620 }
621 IEEE80211_IF_FILE_RW(active_links);
622 
623 IEEE80211_IF_LINK_FILE(addr, conf->addr, MAC);
624 
625 #ifdef CONFIG_MAC80211_MESH
626 IEEE80211_IF_FILE(estab_plinks, u.mesh.estab_plinks, ATOMIC);
627 
628 /* Mesh stats attributes */
629 IEEE80211_IF_FILE(fwded_mcast, u.mesh.mshstats.fwded_mcast, DEC);
630 IEEE80211_IF_FILE(fwded_unicast, u.mesh.mshstats.fwded_unicast, DEC);
631 IEEE80211_IF_FILE(fwded_frames, u.mesh.mshstats.fwded_frames, DEC);
632 IEEE80211_IF_FILE(dropped_frames_ttl, u.mesh.mshstats.dropped_frames_ttl, DEC);
633 IEEE80211_IF_FILE(dropped_frames_no_route,
634 		  u.mesh.mshstats.dropped_frames_no_route, DEC);
635 
636 /* Mesh parameters */
637 IEEE80211_IF_FILE(dot11MeshMaxRetries,
638 		  u.mesh.mshcfg.dot11MeshMaxRetries, DEC);
639 IEEE80211_IF_FILE(dot11MeshRetryTimeout,
640 		  u.mesh.mshcfg.dot11MeshRetryTimeout, DEC);
641 IEEE80211_IF_FILE(dot11MeshConfirmTimeout,
642 		  u.mesh.mshcfg.dot11MeshConfirmTimeout, DEC);
643 IEEE80211_IF_FILE(dot11MeshHoldingTimeout,
644 		  u.mesh.mshcfg.dot11MeshHoldingTimeout, DEC);
645 IEEE80211_IF_FILE(dot11MeshTTL, u.mesh.mshcfg.dot11MeshTTL, DEC);
646 IEEE80211_IF_FILE(element_ttl, u.mesh.mshcfg.element_ttl, DEC);
647 IEEE80211_IF_FILE(auto_open_plinks, u.mesh.mshcfg.auto_open_plinks, DEC);
648 IEEE80211_IF_FILE(dot11MeshMaxPeerLinks,
649 		  u.mesh.mshcfg.dot11MeshMaxPeerLinks, DEC);
650 IEEE80211_IF_FILE(dot11MeshHWMPactivePathTimeout,
651 		  u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout, DEC);
652 IEEE80211_IF_FILE(dot11MeshHWMPpreqMinInterval,
653 		  u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval, DEC);
654 IEEE80211_IF_FILE(dot11MeshHWMPperrMinInterval,
655 		  u.mesh.mshcfg.dot11MeshHWMPperrMinInterval, DEC);
656 IEEE80211_IF_FILE(dot11MeshHWMPnetDiameterTraversalTime,
657 		  u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime, DEC);
658 IEEE80211_IF_FILE(dot11MeshHWMPmaxPREQretries,
659 		  u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries, DEC);
660 IEEE80211_IF_FILE(path_refresh_time,
661 		  u.mesh.mshcfg.path_refresh_time, DEC);
662 IEEE80211_IF_FILE(min_discovery_timeout,
663 		  u.mesh.mshcfg.min_discovery_timeout, DEC);
664 IEEE80211_IF_FILE(dot11MeshHWMPRootMode,
665 		  u.mesh.mshcfg.dot11MeshHWMPRootMode, DEC);
666 IEEE80211_IF_FILE(dot11MeshGateAnnouncementProtocol,
667 		  u.mesh.mshcfg.dot11MeshGateAnnouncementProtocol, DEC);
668 IEEE80211_IF_FILE(dot11MeshHWMPRannInterval,
669 		  u.mesh.mshcfg.dot11MeshHWMPRannInterval, DEC);
670 IEEE80211_IF_FILE(dot11MeshForwarding, u.mesh.mshcfg.dot11MeshForwarding, DEC);
671 IEEE80211_IF_FILE(rssi_threshold, u.mesh.mshcfg.rssi_threshold, DEC);
672 IEEE80211_IF_FILE(ht_opmode, u.mesh.mshcfg.ht_opmode, DEC);
673 IEEE80211_IF_FILE(dot11MeshHWMPactivePathToRootTimeout,
674 		  u.mesh.mshcfg.dot11MeshHWMPactivePathToRootTimeout, DEC);
675 IEEE80211_IF_FILE(dot11MeshHWMProotInterval,
676 		  u.mesh.mshcfg.dot11MeshHWMProotInterval, DEC);
677 IEEE80211_IF_FILE(dot11MeshHWMPconfirmationInterval,
678 		  u.mesh.mshcfg.dot11MeshHWMPconfirmationInterval, DEC);
679 IEEE80211_IF_FILE(power_mode, u.mesh.mshcfg.power_mode, DEC);
680 IEEE80211_IF_FILE(dot11MeshAwakeWindowDuration,
681 		  u.mesh.mshcfg.dot11MeshAwakeWindowDuration, DEC);
682 IEEE80211_IF_FILE(dot11MeshConnectedToMeshGate,
683 		  u.mesh.mshcfg.dot11MeshConnectedToMeshGate, DEC);
684 IEEE80211_IF_FILE(dot11MeshNolearn, u.mesh.mshcfg.dot11MeshNolearn, DEC);
685 IEEE80211_IF_FILE(dot11MeshConnectedToAuthServer,
686 		  u.mesh.mshcfg.dot11MeshConnectedToAuthServer, DEC);
687 #endif
688 
689 #define DEBUGFS_ADD_MODE(name, mode) \
690 	debugfs_create_file(#name, mode, sdata->vif.debugfs_dir, \
691 			    sdata, &name##_ops)
692 
693 #define DEBUGFS_ADD(name) DEBUGFS_ADD_MODE(name, 0400)
694 
695 static void add_common_files(struct ieee80211_sub_if_data *sdata)
696 {
697 	DEBUGFS_ADD(rc_rateidx_mask_2ghz);
698 	DEBUGFS_ADD(rc_rateidx_mask_5ghz);
699 	DEBUGFS_ADD(rc_rateidx_mcs_mask_2ghz);
700 	DEBUGFS_ADD(rc_rateidx_mcs_mask_5ghz);
701 	DEBUGFS_ADD(rc_rateidx_vht_mcs_mask_2ghz);
702 	DEBUGFS_ADD(rc_rateidx_vht_mcs_mask_5ghz);
703 	DEBUGFS_ADD(hw_queues);
704 
705 	if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
706 	    sdata->vif.type != NL80211_IFTYPE_NAN)
707 		DEBUGFS_ADD(aqm);
708 }
709 
710 static void add_sta_files(struct ieee80211_sub_if_data *sdata)
711 {
712 	DEBUGFS_ADD(bssid);
713 	DEBUGFS_ADD(aid);
714 	DEBUGFS_ADD(beacon_timeout);
715 	DEBUGFS_ADD_MODE(tkip_mic_test, 0200);
716 	DEBUGFS_ADD_MODE(beacon_loss, 0200);
717 	DEBUGFS_ADD_MODE(uapsd_queues, 0600);
718 	DEBUGFS_ADD_MODE(uapsd_max_sp_len, 0600);
719 	DEBUGFS_ADD_MODE(tdls_wider_bw, 0600);
720 	DEBUGFS_ADD_MODE(valid_links, 0200);
721 	DEBUGFS_ADD_MODE(active_links, 0600);
722 }
723 
724 static void add_ap_files(struct ieee80211_sub_if_data *sdata)
725 {
726 	DEBUGFS_ADD(num_mcast_sta);
727 	DEBUGFS_ADD(num_sta_ps);
728 	DEBUGFS_ADD(dtim_count);
729 	DEBUGFS_ADD(num_buffered_multicast);
730 	DEBUGFS_ADD_MODE(tkip_mic_test, 0200);
731 	DEBUGFS_ADD_MODE(multicast_to_unicast, 0600);
732 }
733 
734 static void add_vlan_files(struct ieee80211_sub_if_data *sdata)
735 {
736 	/* add num_mcast_sta_vlan using name num_mcast_sta */
737 	debugfs_create_file("num_mcast_sta", 0400, sdata->vif.debugfs_dir,
738 			    sdata, &num_mcast_sta_vlan_ops);
739 }
740 
741 static void add_ibss_files(struct ieee80211_sub_if_data *sdata)
742 {
743 	DEBUGFS_ADD_MODE(tsf, 0600);
744 }
745 
746 #ifdef CONFIG_MAC80211_MESH
747 
748 static void add_mesh_files(struct ieee80211_sub_if_data *sdata)
749 {
750 	DEBUGFS_ADD_MODE(tsf, 0600);
751 	DEBUGFS_ADD_MODE(estab_plinks, 0400);
752 }
753 
754 static void add_mesh_stats(struct ieee80211_sub_if_data *sdata)
755 {
756 	struct dentry *dir = debugfs_create_dir("mesh_stats",
757 						sdata->vif.debugfs_dir);
758 #define MESHSTATS_ADD(name)\
759 	debugfs_create_file(#name, 0400, dir, sdata, &name##_ops)
760 
761 	MESHSTATS_ADD(fwded_mcast);
762 	MESHSTATS_ADD(fwded_unicast);
763 	MESHSTATS_ADD(fwded_frames);
764 	MESHSTATS_ADD(dropped_frames_ttl);
765 	MESHSTATS_ADD(dropped_frames_no_route);
766 #undef MESHSTATS_ADD
767 }
768 
769 static void add_mesh_config(struct ieee80211_sub_if_data *sdata)
770 {
771 	struct dentry *dir = debugfs_create_dir("mesh_config",
772 						sdata->vif.debugfs_dir);
773 
774 #define MESHPARAMS_ADD(name) \
775 	debugfs_create_file(#name, 0600, dir, sdata, &name##_ops)
776 
777 	MESHPARAMS_ADD(dot11MeshMaxRetries);
778 	MESHPARAMS_ADD(dot11MeshRetryTimeout);
779 	MESHPARAMS_ADD(dot11MeshConfirmTimeout);
780 	MESHPARAMS_ADD(dot11MeshHoldingTimeout);
781 	MESHPARAMS_ADD(dot11MeshTTL);
782 	MESHPARAMS_ADD(element_ttl);
783 	MESHPARAMS_ADD(auto_open_plinks);
784 	MESHPARAMS_ADD(dot11MeshMaxPeerLinks);
785 	MESHPARAMS_ADD(dot11MeshHWMPactivePathTimeout);
786 	MESHPARAMS_ADD(dot11MeshHWMPpreqMinInterval);
787 	MESHPARAMS_ADD(dot11MeshHWMPperrMinInterval);
788 	MESHPARAMS_ADD(dot11MeshHWMPnetDiameterTraversalTime);
789 	MESHPARAMS_ADD(dot11MeshHWMPmaxPREQretries);
790 	MESHPARAMS_ADD(path_refresh_time);
791 	MESHPARAMS_ADD(min_discovery_timeout);
792 	MESHPARAMS_ADD(dot11MeshHWMPRootMode);
793 	MESHPARAMS_ADD(dot11MeshHWMPRannInterval);
794 	MESHPARAMS_ADD(dot11MeshForwarding);
795 	MESHPARAMS_ADD(dot11MeshGateAnnouncementProtocol);
796 	MESHPARAMS_ADD(rssi_threshold);
797 	MESHPARAMS_ADD(ht_opmode);
798 	MESHPARAMS_ADD(dot11MeshHWMPactivePathToRootTimeout);
799 	MESHPARAMS_ADD(dot11MeshHWMProotInterval);
800 	MESHPARAMS_ADD(dot11MeshHWMPconfirmationInterval);
801 	MESHPARAMS_ADD(power_mode);
802 	MESHPARAMS_ADD(dot11MeshAwakeWindowDuration);
803 	MESHPARAMS_ADD(dot11MeshConnectedToMeshGate);
804 	MESHPARAMS_ADD(dot11MeshNolearn);
805 	MESHPARAMS_ADD(dot11MeshConnectedToAuthServer);
806 #undef MESHPARAMS_ADD
807 }
808 #endif
809 
810 static void add_files(struct ieee80211_sub_if_data *sdata)
811 {
812 	if (!sdata->vif.debugfs_dir)
813 		return;
814 
815 	DEBUGFS_ADD(flags);
816 	DEBUGFS_ADD(state);
817 
818 	if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
819 		add_common_files(sdata);
820 
821 	switch (sdata->vif.type) {
822 	case NL80211_IFTYPE_MESH_POINT:
823 #ifdef CONFIG_MAC80211_MESH
824 		add_mesh_files(sdata);
825 		add_mesh_stats(sdata);
826 		add_mesh_config(sdata);
827 #endif
828 		break;
829 	case NL80211_IFTYPE_STATION:
830 		add_sta_files(sdata);
831 		break;
832 	case NL80211_IFTYPE_ADHOC:
833 		add_ibss_files(sdata);
834 		break;
835 	case NL80211_IFTYPE_AP:
836 		add_ap_files(sdata);
837 		break;
838 	case NL80211_IFTYPE_AP_VLAN:
839 		add_vlan_files(sdata);
840 		break;
841 	default:
842 		break;
843 	}
844 }
845 
846 #undef DEBUGFS_ADD_MODE
847 #undef DEBUGFS_ADD
848 
849 #define DEBUGFS_ADD_MODE(dentry, name, mode) \
850 	debugfs_create_file(#name, mode, dentry, \
851 			    link, &link_##name##_ops)
852 
853 #define DEBUGFS_ADD(dentry, name) DEBUGFS_ADD_MODE(dentry, name, 0400)
854 
855 static void add_link_files(struct ieee80211_link_data *link,
856 			   struct dentry *dentry)
857 {
858 	DEBUGFS_ADD(dentry, txpower);
859 	DEBUGFS_ADD(dentry, user_power_level);
860 	DEBUGFS_ADD(dentry, ap_power_level);
861 
862 	switch (link->sdata->vif.type) {
863 	case NL80211_IFTYPE_STATION:
864 		DEBUGFS_ADD_MODE(dentry, smps, 0600);
865 		break;
866 	default:
867 		break;
868 	}
869 }
870 
871 void ieee80211_debugfs_add_netdev(struct ieee80211_sub_if_data *sdata)
872 {
873 	char buf[10+IFNAMSIZ];
874 
875 	sprintf(buf, "netdev:%s", sdata->name);
876 	sdata->vif.debugfs_dir = debugfs_create_dir(buf,
877 		sdata->local->hw.wiphy->debugfsdir);
878 	sdata->debugfs.subdir_stations = debugfs_create_dir("stations",
879 							sdata->vif.debugfs_dir);
880 	add_files(sdata);
881 
882 	if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
883 		add_link_files(&sdata->deflink, sdata->vif.debugfs_dir);
884 }
885 
886 void ieee80211_debugfs_remove_netdev(struct ieee80211_sub_if_data *sdata)
887 {
888 	if (!sdata->vif.debugfs_dir)
889 		return;
890 
891 	debugfs_remove_recursive(sdata->vif.debugfs_dir);
892 	sdata->vif.debugfs_dir = NULL;
893 	sdata->debugfs.subdir_stations = NULL;
894 }
895 
896 void ieee80211_debugfs_rename_netdev(struct ieee80211_sub_if_data *sdata)
897 {
898 	struct dentry *dir;
899 	char buf[10 + IFNAMSIZ];
900 
901 	dir = sdata->vif.debugfs_dir;
902 
903 	if (IS_ERR_OR_NULL(dir))
904 		return;
905 
906 	sprintf(buf, "netdev:%s", sdata->name);
907 	debugfs_rename(dir->d_parent, dir, dir->d_parent, buf);
908 }
909 
910 void ieee80211_link_debugfs_add(struct ieee80211_link_data *link)
911 {
912 	char link_dir_name[10];
913 
914 	if (WARN_ON(!link->sdata->vif.debugfs_dir))
915 		return;
916 
917 	/* For now, this should not be called for non-MLO capable drivers */
918 	if (WARN_ON(!(link->sdata->local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO)))
919 		return;
920 
921 	snprintf(link_dir_name, sizeof(link_dir_name),
922 		 "link-%d", link->link_id);
923 
924 	link->debugfs_dir =
925 		debugfs_create_dir(link_dir_name,
926 				   link->sdata->vif.debugfs_dir);
927 
928 	DEBUGFS_ADD(link->debugfs_dir, addr);
929 	add_link_files(link, link->debugfs_dir);
930 }
931 
932 void ieee80211_link_debugfs_remove(struct ieee80211_link_data *link)
933 {
934 	if (!link->sdata->vif.debugfs_dir || !link->debugfs_dir) {
935 		link->debugfs_dir = NULL;
936 		return;
937 	}
938 
939 	if (link->debugfs_dir == link->sdata->vif.debugfs_dir) {
940 		WARN_ON(link != &link->sdata->deflink);
941 		link->debugfs_dir = NULL;
942 		return;
943 	}
944 
945 	debugfs_remove_recursive(link->debugfs_dir);
946 	link->debugfs_dir = NULL;
947 }
948 
949 void ieee80211_link_debugfs_drv_add(struct ieee80211_link_data *link)
950 {
951 	if (WARN_ON(!link->debugfs_dir))
952 		return;
953 
954 	drv_link_add_debugfs(link->sdata->local, link->sdata,
955 			     link->conf, link->debugfs_dir);
956 }
957 
958 void ieee80211_link_debugfs_drv_remove(struct ieee80211_link_data *link)
959 {
960 	if (!link || !link->debugfs_dir)
961 		return;
962 
963 	if (WARN_ON(link->debugfs_dir == link->sdata->vif.debugfs_dir))
964 		return;
965 
966 	/* Recreate the directory excluding the driver data */
967 	debugfs_remove_recursive(link->debugfs_dir);
968 	link->debugfs_dir = NULL;
969 
970 	ieee80211_link_debugfs_add(link);
971 }
972