br_sysfs_if.c 8.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *	Sysfs attributes of bridge ports
 *	Linux ethernet bridge
 *
 *	Authors:
 *	Stephen Hemminger		<shemminger@osdl.org>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

14
#include <linux/capability.h>
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23 24 25
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/if_bridge.h>
#include <linux/rtnetlink.h>
#include <linux/spinlock.h>

#include "br_private.h"

struct brport_attribute {
	struct attribute	attr;
	ssize_t (*show)(struct net_bridge_port *, char *);
26
	int (*store)(struct net_bridge_port *, unsigned long);
L
Linus Torvalds 已提交
27 28
};

29
#define BRPORT_ATTR(_name, _mode, _show, _store)		\
30
const struct brport_attribute brport_attr_##_name = { 	        \
L
Linus Torvalds 已提交
31
	.attr = {.name = __stringify(_name), 			\
32
		 .mode = _mode },				\
L
Linus Torvalds 已提交
33 34 35 36
	.show	= _show,					\
	.store	= _store,					\
};

37 38 39 40 41 42 43
#define BRPORT_ATTR_FLAG(_name, _mask)				\
static ssize_t show_##_name(struct net_bridge_port *p, char *buf) \
{								\
	return sprintf(buf, "%d\n", !!(p->flags & _mask));	\
}								\
static int store_##_name(struct net_bridge_port *p, unsigned long v) \
{								\
44
	return store_flag(p, v, _mask);				\
45 46 47 48
}								\
static BRPORT_ATTR(_name, S_IRUGO | S_IWUSR,			\
		   show_##_name, store_##_name)

49 50 51
static int store_flag(struct net_bridge_port *p, unsigned long v,
		      unsigned long mask)
{
52 53 54
	unsigned long flags;

	flags = p->flags;
55 56 57 58 59 60 61 62

	if (v)
		flags |= mask;
	else
		flags &= ~mask;

	if (flags != p->flags) {
		p->flags = flags;
63
		br_port_flags_change(p, mask);
64 65 66
	}
	return 0;
}
67

L
Linus Torvalds 已提交
68 69 70 71
static ssize_t show_path_cost(struct net_bridge_port *p, char *buf)
{
	return sprintf(buf, "%d\n", p->path_cost);
}
72

L
Linus Torvalds 已提交
73
static BRPORT_ATTR(path_cost, S_IRUGO | S_IWUSR,
74
		   show_path_cost, br_stp_set_path_cost);
L
Linus Torvalds 已提交
75 76 77 78 79

static ssize_t show_priority(struct net_bridge_port *p, char *buf)
{
	return sprintf(buf, "%d\n", p->priority);
}
80

L
Linus Torvalds 已提交
81
static BRPORT_ATTR(priority, S_IRUGO | S_IWUSR,
82
			 show_priority, br_stp_set_port_priority);
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

static ssize_t show_designated_root(struct net_bridge_port *p, char *buf)
{
	return br_show_bridge_id(buf, &p->designated_root);
}
static BRPORT_ATTR(designated_root, S_IRUGO, show_designated_root, NULL);

static ssize_t show_designated_bridge(struct net_bridge_port *p, char *buf)
{
	return br_show_bridge_id(buf, &p->designated_bridge);
}
static BRPORT_ATTR(designated_bridge, S_IRUGO, show_designated_bridge, NULL);

static ssize_t show_designated_port(struct net_bridge_port *p, char *buf)
{
	return sprintf(buf, "%d\n", p->designated_port);
}
static BRPORT_ATTR(designated_port, S_IRUGO, show_designated_port, NULL);

static ssize_t show_designated_cost(struct net_bridge_port *p, char *buf)
{
	return sprintf(buf, "%d\n", p->designated_cost);
}
static BRPORT_ATTR(designated_cost, S_IRUGO, show_designated_cost, NULL);

static ssize_t show_port_id(struct net_bridge_port *p, char *buf)
{
	return sprintf(buf, "0x%x\n", p->port_id);
}
static BRPORT_ATTR(port_id, S_IRUGO, show_port_id, NULL);

static ssize_t show_port_no(struct net_bridge_port *p, char *buf)
{
	return sprintf(buf, "0x%x\n", p->port_no);
}

static BRPORT_ATTR(port_no, S_IRUGO, show_port_no, NULL);

static ssize_t show_change_ack(struct net_bridge_port *p, char *buf)
{
	return sprintf(buf, "%d\n", p->topology_change_ack);
}
static BRPORT_ATTR(change_ack, S_IRUGO, show_change_ack, NULL);

static ssize_t show_config_pending(struct net_bridge_port *p, char *buf)
{
	return sprintf(buf, "%d\n", p->config_pending);
}
static BRPORT_ATTR(config_pending, S_IRUGO, show_config_pending, NULL);

static ssize_t show_port_state(struct net_bridge_port *p, char *buf)
{
	return sprintf(buf, "%d\n", p->state);
}
static BRPORT_ATTR(state, S_IRUGO, show_port_state, NULL);

static ssize_t show_message_age_timer(struct net_bridge_port *p,
					    char *buf)
{
	return sprintf(buf, "%ld\n", br_timer_value(&p->message_age_timer));
}
static BRPORT_ATTR(message_age_timer, S_IRUGO, show_message_age_timer, NULL);

static ssize_t show_forward_delay_timer(struct net_bridge_port *p,
					    char *buf)
{
	return sprintf(buf, "%ld\n", br_timer_value(&p->forward_delay_timer));
}
static BRPORT_ATTR(forward_delay_timer, S_IRUGO, show_forward_delay_timer, NULL);

static ssize_t show_hold_timer(struct net_bridge_port *p,
					    char *buf)
{
	return sprintf(buf, "%ld\n", br_timer_value(&p->hold_timer));
}
static BRPORT_ATTR(hold_timer, S_IRUGO, show_hold_timer, NULL);

160
static int store_flush(struct net_bridge_port *p, unsigned long v)
161
{
162
	br_fdb_delete_by_port(p->br, p, 0, 0); // Don't delete local entry
163 164 165 166
	return 0;
}
static BRPORT_ATTR(flush, S_IWUSR, NULL, store_flush);

167
BRPORT_ATTR_FLAG(hairpin_mode, BR_HAIRPIN_MODE);
168
BRPORT_ATTR_FLAG(bpdu_guard, BR_BPDU_GUARD);
S
stephen hemminger 已提交
169
BRPORT_ATTR_FLAG(root_block, BR_ROOT_BLOCK);
170
BRPORT_ATTR_FLAG(learning, BR_LEARNING);
171
BRPORT_ATTR_FLAG(unicast_flood, BR_FLOOD);
172
BRPORT_ATTR_FLAG(proxyarp, BR_PROXYARP);
173
BRPORT_ATTR_FLAG(proxyarp_wifi, BR_PROXYARP_WIFI);
174
BRPORT_ATTR_FLAG(multicast_flood, BR_MCAST_FLOOD);
175

176 177 178 179 180 181
#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
static ssize_t show_multicast_router(struct net_bridge_port *p, char *buf)
{
	return sprintf(buf, "%d\n", p->multicast_router);
}

182
static int store_multicast_router(struct net_bridge_port *p,
183 184 185 186 187 188
				      unsigned long v)
{
	return br_multicast_set_port_router(p, v);
}
static BRPORT_ATTR(multicast_router, S_IRUGO | S_IWUSR, show_multicast_router,
		   store_multicast_router);
189

190
BRPORT_ATTR_FLAG(multicast_fast_leave, BR_MULTICAST_FAST_LEAVE);
191 192
#endif

193
static const struct brport_attribute *brport_attrs[] = {
L
Linus Torvalds 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207
	&brport_attr_path_cost,
	&brport_attr_priority,
	&brport_attr_port_id,
	&brport_attr_port_no,
	&brport_attr_designated_root,
	&brport_attr_designated_bridge,
	&brport_attr_designated_port,
	&brport_attr_designated_cost,
	&brport_attr_state,
	&brport_attr_change_ack,
	&brport_attr_config_pending,
	&brport_attr_message_age_timer,
	&brport_attr_forward_delay_timer,
	&brport_attr_hold_timer,
208
	&brport_attr_flush,
209
	&brport_attr_hairpin_mode,
210
	&brport_attr_bpdu_guard,
S
stephen hemminger 已提交
211
	&brport_attr_root_block,
212
	&brport_attr_learning,
213
	&brport_attr_unicast_flood,
214 215
#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
	&brport_attr_multicast_router,
216
	&brport_attr_multicast_fast_leave,
217
#endif
218
	&brport_attr_proxyarp,
219
	&brport_attr_proxyarp_wifi,
220
	&brport_attr_multicast_flood,
L
Linus Torvalds 已提交
221 222 223 224 225 226
	NULL
};

#define to_brport_attr(_at) container_of(_at, struct brport_attribute, attr)
#define to_brport(obj)	container_of(obj, struct net_bridge_port, kobj)

227 228
static ssize_t brport_show(struct kobject *kobj,
			   struct attribute *attr, char *buf)
L
Linus Torvalds 已提交
229
{
230 231
	struct brport_attribute *brport_attr = to_brport_attr(attr);
	struct net_bridge_port *p = to_brport(kobj);
L
Linus Torvalds 已提交
232 233 234 235

	return brport_attr->show(p, buf);
}

236 237 238
static ssize_t brport_store(struct kobject *kobj,
			    struct attribute *attr,
			    const char *buf, size_t count)
L
Linus Torvalds 已提交
239
{
240 241
	struct brport_attribute *brport_attr = to_brport_attr(attr);
	struct net_bridge_port *p = to_brport(kobj);
L
Linus Torvalds 已提交
242 243 244 245
	ssize_t ret = -EINVAL;
	char *endp;
	unsigned long val;

246
	if (!ns_capable(dev_net(p->dev)->user_ns, CAP_NET_ADMIN))
L
Linus Torvalds 已提交
247 248 249 250
		return -EPERM;

	val = simple_strtoul(buf, &endp, 0);
	if (endp != buf) {
251 252
		if (!rtnl_trylock())
			return restart_syscall();
L
Linus Torvalds 已提交
253 254 255 256
		if (p->dev && p->br && brport_attr->store) {
			spin_lock_bh(&p->br->lock);
			ret = brport_attr->store(p, val);
			spin_unlock_bh(&p->br->lock);
257 258
			if (!ret) {
				br_ifinfo_notify(RTM_NEWLINK, p);
L
Linus Torvalds 已提交
259
				ret = count;
260
			}
L
Linus Torvalds 已提交
261 262 263 264 265 266
		}
		rtnl_unlock();
	}
	return ret;
}

267
const struct sysfs_ops brport_sysfs_ops = {
L
Linus Torvalds 已提交
268 269 270 271 272 273 274
	.show = brport_show,
	.store = brport_store,
};

/*
 * Add sysfs entries to ethernet device added to a bridge.
 * Creates a brport subdirectory with bridge attributes.
275
 * Puts symlink in bridge's brif subdirectory
L
Linus Torvalds 已提交
276 277 278 279
 */
int br_sysfs_addif(struct net_bridge_port *p)
{
	struct net_bridge *br = p->br;
280
	const struct brport_attribute **a;
L
Linus Torvalds 已提交
281 282
	int err;

283
	err = sysfs_create_link(&p->kobj, &br->dev->dev.kobj,
L
Linus Torvalds 已提交
284 285
				SYSFS_BRIDGE_PORT_LINK);
	if (err)
286
		return err;
L
Linus Torvalds 已提交
287 288 289 290

	for (a = brport_attrs; *a; ++a) {
		err = sysfs_create_file(&p->kobj, &((*a)->attr));
		if (err)
291
			return err;
L
Linus Torvalds 已提交
292 293
	}

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	strlcpy(p->sysfs_name, p->dev->name, IFNAMSIZ);
	return sysfs_create_link(br->ifobj, &p->kobj, p->sysfs_name);
}

/* Rename bridge's brif symlink */
int br_sysfs_renameif(struct net_bridge_port *p)
{
	struct net_bridge *br = p->br;
	int err;

	/* If a rename fails, the rollback will cause another
	 * rename call with the existing name.
	 */
	if (!strncmp(p->sysfs_name, p->dev->name, IFNAMSIZ))
		return 0;

	err = sysfs_rename_link(br->ifobj, &p->kobj,
				p->sysfs_name, p->dev->name);
	if (err)
		netdev_notice(br->dev, "unable to rename link %s to %s",
			      p->sysfs_name, p->dev->name);
	else
		strlcpy(p->sysfs_name, p->dev->name, IFNAMSIZ);

L
Linus Torvalds 已提交
318 319
	return err;
}