br_netlink.c 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *	Bridge netlink control interface
 *
 *	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.
 */

#include <linux/kernel.h>
14
#include <net/rtnetlink.h>
15
#include <net/net_namespace.h>
16
#include <net/sock.h>
17 18
#include "br_private.h"

19 20 21 22 23 24 25 26 27 28 29 30
static inline size_t br_nlmsg_size(void)
{
	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
	       + nla_total_size(4) /* IFLA_MASTER */
	       + nla_total_size(4) /* IFLA_MTU */
	       + nla_total_size(4) /* IFLA_LINK */
	       + nla_total_size(1) /* IFLA_OPERSTATE */
	       + nla_total_size(1); /* IFLA_PROTINFO */
}

31 32 33 34 35 36 37 38 39
/*
 * Create one netlink message for one interface
 * Contains port and master info as well as carrier and bridge state.
 */
static int br_fill_ifinfo(struct sk_buff *skb, const struct net_bridge_port *port,
			  u32 pid, u32 seq, int event, unsigned int flags)
{
	const struct net_bridge *br = port->br;
	const struct net_device *dev = port->dev;
40
	struct ifinfomsg *hdr;
41 42 43 44 45 46
	struct nlmsghdr *nlh;
	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;

	pr_debug("br_fill_info event %d port %s master %s\n",
		 event, dev->name, br->dev->name);

47 48
	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
	if (nlh == NULL)
49
		return -EMSGSIZE;
50

51 52 53 54 55 56 57
	hdr = nlmsg_data(nlh);
	hdr->ifi_family = AF_BRIDGE;
	hdr->__ifi_pad = 0;
	hdr->ifi_type = dev->type;
	hdr->ifi_index = dev->ifindex;
	hdr->ifi_flags = dev_get_flags(dev);
	hdr->ifi_change = 0;
58

59 60 61 62
	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
	NLA_PUT_U32(skb, IFLA_MASTER, br->dev->ifindex);
	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
	NLA_PUT_U8(skb, IFLA_OPERSTATE, operstate);
63 64

	if (dev->addr_len)
65
		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
66 67

	if (dev->ifindex != dev->iflink)
68
		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
69 70

	if (event == RTM_NEWLINK)
71
		NLA_PUT_U8(skb, IFLA_PROTINFO, port->state);
72

73
	return nlmsg_end(skb, nlh);
74

75
nla_put_failure:
76 77
	nlmsg_cancel(skb, nlh);
	return -EMSGSIZE;
78 79 80 81 82 83 84 85
}

/*
 * Notify listeners of a change in port information
 */
void br_ifinfo_notify(int event, struct net_bridge_port *port)
{
	struct sk_buff *skb;
86
	int err = -ENOBUFS;
87 88

	pr_debug("bridge notify event=%d\n", event);
89
	skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC);
90 91 92 93
	if (skb == NULL)
		goto errout;

	err = br_fill_ifinfo(skb, port, 0, 0, event, 0);
94 95 96 97 98 99
	if (err < 0) {
		/* -EMSGSIZE implies BUG in br_nlmsg_size() */
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(skb);
		goto errout;
	}
100
	err = rtnl_notify(skb, &init_net,0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
101
errout:
S
Stephen Hemminger 已提交
102
	if (err < 0)
103
		rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
104 105 106 107 108 109 110
}

/*
 * Dump information about all ports, in response to GETLINK
 */
static int br_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
{
111
	struct net *net = skb->sk->sk_net;
112 113 114
	struct net_device *dev;
	int idx;

115 116 117
	if (net != &init_net)
		return 0;

118
	idx = 0;
119
	for_each_netdev(&init_net, dev) {
120
		/* not a bridge port */
121 122
		if (dev->br_port == NULL || idx < cb->args[0])
			goto skip;
123

124 125 126
		if (br_fill_ifinfo(skb, dev->br_port, NETLINK_CB(cb->skb).pid,
				   cb->nlh->nlmsg_seq, RTM_NEWLINK,
				   NLM_F_MULTI) < 0)
127
			break;
128
skip:
129 130 131 132 133 134 135 136 137 138 139 140 141 142
		++idx;
	}

	cb->args[0] = idx;

	return skb->len;
}

/*
 * Change state of port (ie from forwarding to blocking etc)
 * Used by spanning tree in user space.
 */
static int br_rtm_setlink(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
{
143
	struct net *net = skb->sk->sk_net;
144 145
	struct ifinfomsg *ifm;
	struct nlattr *protinfo;
146 147 148 149
	struct net_device *dev;
	struct net_bridge_port *p;
	u8 new_state;

150 151 152
	if (net != &init_net)
		return -EINVAL;

153 154 155 156
	if (nlmsg_len(nlh) < sizeof(*ifm))
		return -EINVAL;

	ifm = nlmsg_data(nlh);
157 158 159
	if (ifm->ifi_family != AF_BRIDGE)
		return -EPFNOSUPPORT;

160 161
	protinfo = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_PROTINFO);
	if (!protinfo || nla_len(protinfo) < sizeof(u8))
162 163
		return -EINVAL;

164
	new_state = nla_get_u8(protinfo);
165 166 167
	if (new_state > BR_STATE_BLOCKING)
		return -EINVAL;

168
	dev = __dev_get_by_index(&init_net, ifm->ifi_index);
169 170 171 172 173 174 175 176
	if (!dev)
		return -ENODEV;

	p = dev->br_port;
	if (!p)
		return -EINVAL;

	/* if kernel STP is running, don't allow changes */
177
	if (p->br->stp_enabled == BR_KERNEL_STP)
178 179
		return -EBUSY;

180 181
	if (!netif_running(dev) ||
	    (!netif_carrier_ok(dev) && new_state != BR_STATE_DISABLED))
182 183 184 185 186 187 188 189
		return -ENETDOWN;

	p->state = new_state;
	br_log_state(p);
	return 0;
}


190
int __init br_netlink_init(void)
191
{
192 193 194 195 196 197 198
	if (__rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, br_dump_ifinfo))
		return -ENOBUFS;

	/* Only the first call to __rtnl_register can fail */
	__rtnl_register(PF_BRIDGE, RTM_SETLINK, br_rtm_setlink, NULL);

	return 0;
199 200 201 202
}

void __exit br_netlink_fini(void)
{
203
	rtnl_unregister_all(PF_BRIDGE);
204 205
}