debugfs_netdev.c 12.9 KB
Newer Older
J
Jiri Benc 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2006	Jiri Benc <jbenc@suse.cz>
 * Copyright 2007	Johannes Berg <johannes@sipsolutions.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/if.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
16
#include <linux/slab.h>
J
Jiri Benc 已提交
17 18 19 20
#include <linux/notifier.h>
#include <net/mac80211.h>
#include <net/cfg80211.h>
#include "ieee80211_i.h"
J
Johannes Berg 已提交
21
#include "rate.h"
J
Jiri Benc 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34
#include "debugfs.h"
#include "debugfs_netdev.h"

static ssize_t ieee80211_if_read(
	struct ieee80211_sub_if_data *sdata,
	char __user *userbuf,
	size_t count, loff_t *ppos,
	ssize_t (*format)(const struct ieee80211_sub_if_data *, char *, int))
{
	char buf[70];
	ssize_t ret = -EINVAL;

	read_lock(&dev_base_lock);
35
	if (sdata->dev->reg_state == NETREG_REGISTERED)
J
Jiri Benc 已提交
36 37
		ret = (*format)(sdata, buf, sizeof(buf));
	read_unlock(&dev_base_lock);
38 39 40 41

	if (ret != -EINVAL)
		ret = simple_read_from_buffer(userbuf, count, ppos, buf, ret);

J
Jiri Benc 已提交
42 43 44
	return ret;
}

45 46 47 48 49 50 51
static ssize_t ieee80211_if_write(
	struct ieee80211_sub_if_data *sdata,
	const char __user *userbuf,
	size_t count, loff_t *ppos,
	ssize_t (*write)(struct ieee80211_sub_if_data *, const char *, int))
{
	u8 *buf;
52
	ssize_t ret;
53

54
	buf = kmalloc(count, GFP_KERNEL);
55 56 57
	if (!buf)
		return -ENOMEM;

58
	ret = -EFAULT;
59
	if (copy_from_user(buf, userbuf, count))
60
		goto freebuf;
61

62
	ret = -ENODEV;
63 64 65 66 67
	rtnl_lock();
	if (sdata->dev->reg_state == NETREG_REGISTERED)
		ret = (*write)(sdata, buf, count);
	rtnl_unlock();

68 69
freebuf:
	kfree(buf);
70 71 72
	return ret;
}

J
Jiri Benc 已提交
73 74 75 76 77 78 79 80 81 82 83
#define IEEE80211_IF_FMT(name, field, format_string)			\
static ssize_t ieee80211_if_fmt_##name(					\
	const struct ieee80211_sub_if_data *sdata, char *buf,		\
	int buflen)							\
{									\
	return scnprintf(buf, buflen, format_string, sdata->field);	\
}
#define IEEE80211_IF_FMT_DEC(name, field)				\
		IEEE80211_IF_FMT(name, field, "%d\n")
#define IEEE80211_IF_FMT_HEX(name, field)				\
		IEEE80211_IF_FMT(name, field, "%#x\n")
84 85
#define IEEE80211_IF_FMT_LHEX(name, field)				\
		IEEE80211_IF_FMT(name, field, "%#lx\n")
J
Jiri Benc 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#define IEEE80211_IF_FMT_SIZE(name, field)				\
		IEEE80211_IF_FMT(name, field, "%zd\n")

#define IEEE80211_IF_FMT_ATOMIC(name, field)				\
static ssize_t ieee80211_if_fmt_##name(					\
	const struct ieee80211_sub_if_data *sdata,			\
	char *buf, int buflen)						\
{									\
	return scnprintf(buf, buflen, "%d\n", atomic_read(&sdata->field));\
}

#define IEEE80211_IF_FMT_MAC(name, field)				\
static ssize_t ieee80211_if_fmt_##name(					\
	const struct ieee80211_sub_if_data *sdata, char *buf,		\
	int buflen)							\
{									\
102
	return scnprintf(buf, buflen, "%pM\n", sdata->field);		\
J
Jiri Benc 已提交
103 104
}

105 106 107 108 109 110 111 112
#define IEEE80211_IF_FMT_DEC_DIV_16(name, field)			\
static ssize_t ieee80211_if_fmt_##name(					\
	const struct ieee80211_sub_if_data *sdata,			\
	char *buf, int buflen)						\
{									\
	return scnprintf(buf, buflen, "%d\n", sdata->field / 16);	\
}

113
#define __IEEE80211_IF_FILE(name, _write)				\
J
Jiri Benc 已提交
114 115 116 117 118 119 120 121 122 123
static ssize_t ieee80211_if_read_##name(struct file *file,		\
					char __user *userbuf,		\
					size_t count, loff_t *ppos)	\
{									\
	return ieee80211_if_read(file->private_data,			\
				 userbuf, count, ppos,			\
				 ieee80211_if_fmt_##name);		\
}									\
static const struct file_operations name##_ops = {			\
	.read = ieee80211_if_read_##name,				\
124
	.write = (_write),						\
J
Jiri Benc 已提交
125
	.open = mac80211_open_file_generic,				\
126
	.llseek = generic_file_llseek,					\
J
Jiri Benc 已提交
127 128
}

129 130 131 132 133 134 135 136 137 138 139
#define __IEEE80211_IF_FILE_W(name)					\
static ssize_t ieee80211_if_write_##name(struct file *file,		\
					 const char __user *userbuf,	\
					 size_t count, loff_t *ppos)	\
{									\
	return ieee80211_if_write(file->private_data, userbuf, count,	\
				  ppos, ieee80211_if_parse_##name);	\
}									\
__IEEE80211_IF_FILE(name, ieee80211_if_write_##name)


J
Jiri Benc 已提交
140 141
#define IEEE80211_IF_FILE(name, field, format)				\
		IEEE80211_IF_FMT_##format(name, field)			\
142
		__IEEE80211_IF_FILE(name, NULL)
J
Jiri Benc 已提交
143 144 145

/* common attributes */
IEEE80211_IF_FILE(drop_unencrypted, drop_unencrypted, DEC);
146 147 148 149
IEEE80211_IF_FILE(rc_rateidx_mask_2ghz, rc_rateidx_mask[IEEE80211_BAND_2GHZ],
		  HEX);
IEEE80211_IF_FILE(rc_rateidx_mask_5ghz, rc_rateidx_mask[IEEE80211_BAND_5GHZ],
		  HEX);
150 151
IEEE80211_IF_FILE(flags, flags, HEX);
IEEE80211_IF_FILE(state, state, LHEX);
152
IEEE80211_IF_FILE(channel_type, vif.bss_conf.channel_type, DEC);
J
Jiri Benc 已提交
153

154 155 156
/* STA attributes */
IEEE80211_IF_FILE(bssid, u.mgd.bssid, MAC);
IEEE80211_IF_FILE(aid, u.mgd.aid, DEC);
157 158
IEEE80211_IF_FILE(last_beacon, u.mgd.last_beacon_signal, DEC);
IEEE80211_IF_FILE(ave_beacon, u.mgd.ave_beacon_signal, DEC_DIV_16);
J
Jiri Benc 已提交
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
static int ieee80211_set_smps(struct ieee80211_sub_if_data *sdata,
			      enum ieee80211_smps_mode smps_mode)
{
	struct ieee80211_local *local = sdata->local;
	int err;

	if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_STATIC_SMPS) &&
	    smps_mode == IEEE80211_SMPS_STATIC)
		return -EINVAL;

	/* auto should be dynamic if in PS mode */
	if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS) &&
	    (smps_mode == IEEE80211_SMPS_DYNAMIC ||
	     smps_mode == IEEE80211_SMPS_AUTOMATIC))
		return -EINVAL;

	/* supported only on managed interfaces for now */
	if (sdata->vif.type != NL80211_IFTYPE_STATION)
		return -EOPNOTSUPP;

	mutex_lock(&local->iflist_mtx);
	err = __ieee80211_request_smps(sdata, smps_mode);
	mutex_unlock(&local->iflist_mtx);

	return err;
}

static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
	[IEEE80211_SMPS_AUTOMATIC] = "auto",
	[IEEE80211_SMPS_OFF] = "off",
	[IEEE80211_SMPS_STATIC] = "static",
	[IEEE80211_SMPS_DYNAMIC] = "dynamic",
};

static ssize_t ieee80211_if_fmt_smps(const struct ieee80211_sub_if_data *sdata,
				     char *buf, int buflen)
{
	if (sdata->vif.type != NL80211_IFTYPE_STATION)
		return -EOPNOTSUPP;

	return snprintf(buf, buflen, "request: %s\nused: %s\n",
			smps_modes[sdata->u.mgd.req_smps],
			smps_modes[sdata->u.mgd.ap_smps]);
}

static ssize_t ieee80211_if_parse_smps(struct ieee80211_sub_if_data *sdata,
				       const char *buf, int buflen)
{
	enum ieee80211_smps_mode mode;

	for (mode = 0; mode < IEEE80211_SMPS_NUM_MODES; mode++) {
		if (strncmp(buf, smps_modes[mode], buflen) == 0) {
			int err = ieee80211_set_smps(sdata, mode);
			if (!err)
				return buflen;
			return err;
		}
	}

	return -EINVAL;
}

__IEEE80211_IF_FILE_W(smps);

J
Jiri Benc 已提交
224 225 226 227 228 229 230 231 232 233
/* AP attributes */
IEEE80211_IF_FILE(num_sta_ps, u.ap.num_sta_ps, ATOMIC);
IEEE80211_IF_FILE(dtim_count, u.ap.dtim_count, DEC);

static ssize_t ieee80211_if_fmt_num_buffered_multicast(
	const struct ieee80211_sub_if_data *sdata, char *buf, int buflen)
{
	return scnprintf(buf, buflen, "%u\n",
			 skb_queue_len(&sdata->u.ap.ps_bc_buf));
}
234
__IEEE80211_IF_FILE(num_buffered_multicast, NULL);
J
Jiri Benc 已提交
235 236 237 238

/* WDS attributes */
IEEE80211_IF_FILE(peer, u.wds.remote_addr, MAC);

239 240
#ifdef CONFIG_MAC80211_MESH
/* Mesh stats attributes */
241 242
IEEE80211_IF_FILE(fwded_mcast, u.mesh.mshstats.fwded_mcast, DEC);
IEEE80211_IF_FILE(fwded_unicast, u.mesh.mshstats.fwded_unicast, DEC);
243 244
IEEE80211_IF_FILE(fwded_frames, u.mesh.mshstats.fwded_frames, DEC);
IEEE80211_IF_FILE(dropped_frames_ttl, u.mesh.mshstats.dropped_frames_ttl, DEC);
245
IEEE80211_IF_FILE(dropped_frames_no_route,
246 247
		u.mesh.mshstats.dropped_frames_no_route, DEC);
IEEE80211_IF_FILE(estab_plinks, u.mesh.mshstats.estab_plinks, ATOMIC);
248 249

/* Mesh parameters */
250 251 252 253 254 255 256 257 258
IEEE80211_IF_FILE(dot11MeshMaxRetries,
		u.mesh.mshcfg.dot11MeshMaxRetries, DEC);
IEEE80211_IF_FILE(dot11MeshRetryTimeout,
		u.mesh.mshcfg.dot11MeshRetryTimeout, DEC);
IEEE80211_IF_FILE(dot11MeshConfirmTimeout,
		u.mesh.mshcfg.dot11MeshConfirmTimeout, DEC);
IEEE80211_IF_FILE(dot11MeshHoldingTimeout,
		u.mesh.mshcfg.dot11MeshHoldingTimeout, DEC);
IEEE80211_IF_FILE(dot11MeshTTL, u.mesh.mshcfg.dot11MeshTTL, DEC);
259
IEEE80211_IF_FILE(element_ttl, u.mesh.mshcfg.element_ttl, DEC);
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
IEEE80211_IF_FILE(auto_open_plinks, u.mesh.mshcfg.auto_open_plinks, DEC);
IEEE80211_IF_FILE(dot11MeshMaxPeerLinks,
		u.mesh.mshcfg.dot11MeshMaxPeerLinks, DEC);
IEEE80211_IF_FILE(dot11MeshHWMPactivePathTimeout,
		u.mesh.mshcfg.dot11MeshHWMPactivePathTimeout, DEC);
IEEE80211_IF_FILE(dot11MeshHWMPpreqMinInterval,
		u.mesh.mshcfg.dot11MeshHWMPpreqMinInterval, DEC);
IEEE80211_IF_FILE(dot11MeshHWMPnetDiameterTraversalTime,
		u.mesh.mshcfg.dot11MeshHWMPnetDiameterTraversalTime, DEC);
IEEE80211_IF_FILE(dot11MeshHWMPmaxPREQretries,
		u.mesh.mshcfg.dot11MeshHWMPmaxPREQretries, DEC);
IEEE80211_IF_FILE(path_refresh_time,
		u.mesh.mshcfg.path_refresh_time, DEC);
IEEE80211_IF_FILE(min_discovery_timeout,
		u.mesh.mshcfg.min_discovery_timeout, DEC);
275 276
IEEE80211_IF_FILE(dot11MeshHWMPRootMode,
		u.mesh.mshcfg.dot11MeshHWMPRootMode, DEC);
277 278 279
#endif


280
#define DEBUGFS_ADD(name) \
281 282
	debugfs_create_file(#name, 0400, sdata->debugfs.dir, \
			    sdata, &name##_ops);
J
Jiri Benc 已提交
283

284 285 286 287
#define DEBUGFS_ADD_MODE(name, mode) \
	debugfs_create_file(#name, mode, sdata->debugfs.dir, \
			    sdata, &name##_ops);

J
Jiri Benc 已提交
288 289
static void add_sta_files(struct ieee80211_sub_if_data *sdata)
{
290
	DEBUGFS_ADD(drop_unencrypted);
291 292
	DEBUGFS_ADD(flags);
	DEBUGFS_ADD(state);
293
	DEBUGFS_ADD(channel_type);
294 295
	DEBUGFS_ADD(rc_rateidx_mask_2ghz);
	DEBUGFS_ADD(rc_rateidx_mask_5ghz);
296

297 298
	DEBUGFS_ADD(bssid);
	DEBUGFS_ADD(aid);
299 300
	DEBUGFS_ADD(last_beacon);
	DEBUGFS_ADD(ave_beacon);
301
	DEBUGFS_ADD_MODE(smps, 0600);
J
Jiri Benc 已提交
302 303 304 305
}

static void add_ap_files(struct ieee80211_sub_if_data *sdata)
{
306
	DEBUGFS_ADD(drop_unencrypted);
307 308
	DEBUGFS_ADD(flags);
	DEBUGFS_ADD(state);
309
	DEBUGFS_ADD(channel_type);
310 311
	DEBUGFS_ADD(rc_rateidx_mask_2ghz);
	DEBUGFS_ADD(rc_rateidx_mask_5ghz);
312

313 314 315
	DEBUGFS_ADD(num_sta_ps);
	DEBUGFS_ADD(dtim_count);
	DEBUGFS_ADD(num_buffered_multicast);
J
Jiri Benc 已提交
316 317 318 319
}

static void add_wds_files(struct ieee80211_sub_if_data *sdata)
{
320
	DEBUGFS_ADD(drop_unencrypted);
321 322
	DEBUGFS_ADD(flags);
	DEBUGFS_ADD(state);
323
	DEBUGFS_ADD(channel_type);
324 325
	DEBUGFS_ADD(rc_rateidx_mask_2ghz);
	DEBUGFS_ADD(rc_rateidx_mask_5ghz);
326

327
	DEBUGFS_ADD(peer);
J
Jiri Benc 已提交
328 329 330 331
}

static void add_vlan_files(struct ieee80211_sub_if_data *sdata)
{
332
	DEBUGFS_ADD(drop_unencrypted);
333 334
	DEBUGFS_ADD(flags);
	DEBUGFS_ADD(state);
335
	DEBUGFS_ADD(channel_type);
336 337
	DEBUGFS_ADD(rc_rateidx_mask_2ghz);
	DEBUGFS_ADD(rc_rateidx_mask_5ghz);
J
Jiri Benc 已提交
338 339 340 341
}

static void add_monitor_files(struct ieee80211_sub_if_data *sdata)
{
342 343
	DEBUGFS_ADD(flags);
	DEBUGFS_ADD(state);
344
	DEBUGFS_ADD(channel_type);
J
Jiri Benc 已提交
345 346
}

347 348 349 350
#ifdef CONFIG_MAC80211_MESH

static void add_mesh_stats(struct ieee80211_sub_if_data *sdata)
{
351 352 353 354 355 356
	struct dentry *dir = debugfs_create_dir("mesh_stats",
						sdata->debugfs.dir);

#define MESHSTATS_ADD(name)\
	debugfs_create_file(#name, 0400, dir, sdata, &name##_ops);

357 358
	MESHSTATS_ADD(fwded_mcast);
	MESHSTATS_ADD(fwded_unicast);
359 360 361 362
	MESHSTATS_ADD(fwded_frames);
	MESHSTATS_ADD(dropped_frames_ttl);
	MESHSTATS_ADD(dropped_frames_no_route);
	MESHSTATS_ADD(estab_plinks);
363
#undef MESHSTATS_ADD
364 365 366 367
}

static void add_mesh_config(struct ieee80211_sub_if_data *sdata)
{
368 369 370 371 372 373
	struct dentry *dir = debugfs_create_dir("mesh_config",
						sdata->debugfs.dir);

#define MESHPARAMS_ADD(name) \
	debugfs_create_file(#name, 0600, dir, sdata, &name##_ops);

374 375 376 377 378
	MESHPARAMS_ADD(dot11MeshMaxRetries);
	MESHPARAMS_ADD(dot11MeshRetryTimeout);
	MESHPARAMS_ADD(dot11MeshConfirmTimeout);
	MESHPARAMS_ADD(dot11MeshHoldingTimeout);
	MESHPARAMS_ADD(dot11MeshTTL);
379
	MESHPARAMS_ADD(element_ttl);
380 381 382 383 384 385 386 387
	MESHPARAMS_ADD(auto_open_plinks);
	MESHPARAMS_ADD(dot11MeshMaxPeerLinks);
	MESHPARAMS_ADD(dot11MeshHWMPactivePathTimeout);
	MESHPARAMS_ADD(dot11MeshHWMPpreqMinInterval);
	MESHPARAMS_ADD(dot11MeshHWMPnetDiameterTraversalTime);
	MESHPARAMS_ADD(dot11MeshHWMPmaxPREQretries);
	MESHPARAMS_ADD(path_refresh_time);
	MESHPARAMS_ADD(min_discovery_timeout);
388 389

#undef MESHPARAMS_ADD
390 391 392
}
#endif

J
Jiri Benc 已提交
393 394
static void add_files(struct ieee80211_sub_if_data *sdata)
{
395
	if (!sdata->debugfs.dir)
J
Jiri Benc 已提交
396 397
		return;

398
	switch (sdata->vif.type) {
399
	case NL80211_IFTYPE_MESH_POINT:
400 401 402 403
#ifdef CONFIG_MAC80211_MESH
		add_mesh_stats(sdata);
		add_mesh_config(sdata);
#endif
404
		break;
405
	case NL80211_IFTYPE_STATION:
J
Jiri Benc 已提交
406 407
		add_sta_files(sdata);
		break;
408 409 410
	case NL80211_IFTYPE_ADHOC:
		/* XXX */
		break;
411
	case NL80211_IFTYPE_AP:
J
Jiri Benc 已提交
412 413
		add_ap_files(sdata);
		break;
414
	case NL80211_IFTYPE_WDS:
J
Jiri Benc 已提交
415 416
		add_wds_files(sdata);
		break;
417
	case NL80211_IFTYPE_MONITOR:
J
Jiri Benc 已提交
418 419
		add_monitor_files(sdata);
		break;
420
	case NL80211_IFTYPE_AP_VLAN:
J
Jiri Benc 已提交
421 422 423 424 425 426 427 428 429 430 431
		add_vlan_files(sdata);
		break;
	default:
		break;
	}
}

void ieee80211_debugfs_add_netdev(struct ieee80211_sub_if_data *sdata)
{
	char buf[10+IFNAMSIZ];

432
	sprintf(buf, "netdev:%s", sdata->name);
433
	sdata->debugfs.dir = debugfs_create_dir(buf,
J
Jiri Benc 已提交
434
		sdata->local->hw.wiphy->debugfsdir);
435 436 437
	if (sdata->debugfs.dir)
		sdata->debugfs.subdir_stations = debugfs_create_dir("stations",
			sdata->debugfs.dir);
438
	add_files(sdata);
J
Jiri Benc 已提交
439 440 441 442
}

void ieee80211_debugfs_remove_netdev(struct ieee80211_sub_if_data *sdata)
{
443 444 445 446 447
	if (!sdata->debugfs.dir)
		return;

	debugfs_remove_recursive(sdata->debugfs.dir);
	sdata->debugfs.dir = NULL;
J
Jiri Benc 已提交
448 449
}

450
void ieee80211_debugfs_rename_netdev(struct ieee80211_sub_if_data *sdata)
J
Jiri Benc 已提交
451
{
452
	struct dentry *dir;
453
	char buf[10 + IFNAMSIZ];
454

455
	dir = sdata->debugfs.dir;
456 457

	if (!dir)
458
		return;
459

460
	sprintf(buf, "netdev:%s", sdata->name);
461 462 463
	if (!debugfs_rename(dir->d_parent, dir, dir->d_parent, buf))
		printk(KERN_ERR "mac80211: debugfs: failed to rename debugfs "
		       "dir to %s\n", buf);
J
Jiri Benc 已提交
464
}