xref: /linux/net/bridge/br_netlink.c (revision e190bfe56841551b1ad5abb42ebd0c4798cc8c01)
1 /*
2  *	Bridge netlink control interface
3  *
4  *	Authors:
5  *	Stephen Hemminger		<shemminger@osdl.org>
6  *
7  *	This program is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU General Public License
9  *	as published by the Free Software Foundation; either version
10  *	2 of the License, or (at your option) any later version.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <net/rtnetlink.h>
16 #include <net/net_namespace.h>
17 #include <net/sock.h>
18 #include "br_private.h"
19 
20 static inline size_t br_nlmsg_size(void)
21 {
22 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
23 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
24 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
25 	       + nla_total_size(4) /* IFLA_MASTER */
26 	       + nla_total_size(4) /* IFLA_MTU */
27 	       + nla_total_size(4) /* IFLA_LINK */
28 	       + nla_total_size(1) /* IFLA_OPERSTATE */
29 	       + nla_total_size(1); /* IFLA_PROTINFO */
30 }
31 
32 /*
33  * Create one netlink message for one interface
34  * Contains port and master info as well as carrier and bridge state.
35  */
36 static int br_fill_ifinfo(struct sk_buff *skb, const struct net_bridge_port *port,
37 			  u32 pid, u32 seq, int event, unsigned int flags)
38 {
39 	const struct net_bridge *br = port->br;
40 	const struct net_device *dev = port->dev;
41 	struct ifinfomsg *hdr;
42 	struct nlmsghdr *nlh;
43 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
44 
45 	br_debug(br, "br_fill_info event %d port %s master %s\n",
46 		     event, dev->name, br->dev->name);
47 
48 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
49 	if (nlh == NULL)
50 		return -EMSGSIZE;
51 
52 	hdr = nlmsg_data(nlh);
53 	hdr->ifi_family = AF_BRIDGE;
54 	hdr->__ifi_pad = 0;
55 	hdr->ifi_type = dev->type;
56 	hdr->ifi_index = dev->ifindex;
57 	hdr->ifi_flags = dev_get_flags(dev);
58 	hdr->ifi_change = 0;
59 
60 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
61 	NLA_PUT_U32(skb, IFLA_MASTER, br->dev->ifindex);
62 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
63 	NLA_PUT_U8(skb, IFLA_OPERSTATE, operstate);
64 
65 	if (dev->addr_len)
66 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
67 
68 	if (dev->ifindex != dev->iflink)
69 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
70 
71 	if (event == RTM_NEWLINK)
72 		NLA_PUT_U8(skb, IFLA_PROTINFO, port->state);
73 
74 	return nlmsg_end(skb, nlh);
75 
76 nla_put_failure:
77 	nlmsg_cancel(skb, nlh);
78 	return -EMSGSIZE;
79 }
80 
81 /*
82  * Notify listeners of a change in port information
83  */
84 void br_ifinfo_notify(int event, struct net_bridge_port *port)
85 {
86 	struct net *net = dev_net(port->dev);
87 	struct sk_buff *skb;
88 	int err = -ENOBUFS;
89 
90 	br_debug(port->br, "port %u(%s) event %d\n",
91 		 (unsigned)port->port_no, port->dev->name, event);
92 
93 	skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC);
94 	if (skb == NULL)
95 		goto errout;
96 
97 	err = br_fill_ifinfo(skb, port, 0, 0, event, 0);
98 	if (err < 0) {
99 		/* -EMSGSIZE implies BUG in br_nlmsg_size() */
100 		WARN_ON(err == -EMSGSIZE);
101 		kfree_skb(skb);
102 		goto errout;
103 	}
104 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
105 	return;
106 errout:
107 	if (err < 0)
108 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
109 }
110 
111 /*
112  * Dump information about all ports, in response to GETLINK
113  */
114 static int br_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
115 {
116 	struct net *net = sock_net(skb->sk);
117 	struct net_device *dev;
118 	int idx;
119 
120 	idx = 0;
121 	for_each_netdev(net, dev) {
122 		/* not a bridge port */
123 		if (dev->br_port == NULL || idx < cb->args[0])
124 			goto skip;
125 
126 		if (br_fill_ifinfo(skb, dev->br_port, NETLINK_CB(cb->skb).pid,
127 				   cb->nlh->nlmsg_seq, RTM_NEWLINK,
128 				   NLM_F_MULTI) < 0)
129 			break;
130 skip:
131 		++idx;
132 	}
133 
134 	cb->args[0] = idx;
135 
136 	return skb->len;
137 }
138 
139 /*
140  * Change state of port (ie from forwarding to blocking etc)
141  * Used by spanning tree in user space.
142  */
143 static int br_rtm_setlink(struct sk_buff *skb,  struct nlmsghdr *nlh, void *arg)
144 {
145 	struct net *net = sock_net(skb->sk);
146 	struct ifinfomsg *ifm;
147 	struct nlattr *protinfo;
148 	struct net_device *dev;
149 	struct net_bridge_port *p;
150 	u8 new_state;
151 
152 	if (nlmsg_len(nlh) < sizeof(*ifm))
153 		return -EINVAL;
154 
155 	ifm = nlmsg_data(nlh);
156 	if (ifm->ifi_family != AF_BRIDGE)
157 		return -EPFNOSUPPORT;
158 
159 	protinfo = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_PROTINFO);
160 	if (!protinfo || nla_len(protinfo) < sizeof(u8))
161 		return -EINVAL;
162 
163 	new_state = nla_get_u8(protinfo);
164 	if (new_state > BR_STATE_BLOCKING)
165 		return -EINVAL;
166 
167 	dev = __dev_get_by_index(net, ifm->ifi_index);
168 	if (!dev)
169 		return -ENODEV;
170 
171 	p = dev->br_port;
172 	if (!p)
173 		return -EINVAL;
174 
175 	/* if kernel STP is running, don't allow changes */
176 	if (p->br->stp_enabled == BR_KERNEL_STP)
177 		return -EBUSY;
178 
179 	if (!netif_running(dev) ||
180 	    (!netif_carrier_ok(dev) && new_state != BR_STATE_DISABLED))
181 		return -ENETDOWN;
182 
183 	p->state = new_state;
184 	br_log_state(p);
185 	return 0;
186 }
187 
188 
189 int __init br_netlink_init(void)
190 {
191 	if (__rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, br_dump_ifinfo))
192 		return -ENOBUFS;
193 
194 	/* Only the first call to __rtnl_register can fail */
195 	__rtnl_register(PF_BRIDGE, RTM_SETLINK, br_rtm_setlink, NULL);
196 
197 	return 0;
198 }
199 
200 void __exit br_netlink_fini(void)
201 {
202 	rtnl_unregister_all(PF_BRIDGE);
203 }
204 
205