blob: 850b7d1f3a4133e7ccdff686c5a3ee328c4d1b32 [file] [log] [blame]
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
stephen hemmingerbb900b22011-04-04 14:03:32 +000015#include <linux/etherdevice.h>
Thomas Graf32fe21c2007-03-22 11:59:03 -070016#include <net/rtnetlink.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070017#include <net/net_namespace.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110018#include <net/sock.h>
stephen hemmingerbb900b22011-04-04 14:03:32 +000019
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070020#include "br_private.h"
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +000021#include "br_private_stp.h"
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070022
stephen hemminger25c71c72012-11-13 07:53:05 +000023static inline size_t br_port_info_size(void)
24{
25 return nla_total_size(1) /* IFLA_BRPORT_STATE */
26 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
27 + nla_total_size(4) /* IFLA_BRPORT_COST */
28 + nla_total_size(1) /* IFLA_BRPORT_MODE */
stephen hemmingera2e01a62012-11-13 07:53:07 +000029 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
stephen hemminger1007dd12012-11-13 07:53:08 +000030 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
stephen hemminger25c71c72012-11-13 07:53:05 +000031 + 0;
32}
33
Thomas Graf339bf982006-11-10 14:10:15 -080034static inline size_t br_nlmsg_size(void)
35{
36 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +000037 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
38 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
39 + nla_total_size(4) /* IFLA_MASTER */
40 + nla_total_size(4) /* IFLA_MTU */
41 + nla_total_size(4) /* IFLA_LINK */
42 + nla_total_size(1) /* IFLA_OPERSTATE */
43 + nla_total_size(br_port_info_size()); /* IFLA_PROTINFO */
44}
45
46static int br_port_fill_attrs(struct sk_buff *skb,
47 const struct net_bridge_port *p)
48{
49 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
50
51 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
52 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
53 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +000054 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +000055 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -050056 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
57 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)))
stephen hemminger25c71c72012-11-13 07:53:05 +000058 return -EMSGSIZE;
59
60 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -080061}
62
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070063/*
64 * Create one netlink message for one interface
65 * Contains port and master info as well as carrier and bridge state.
66 */
67static int br_fill_ifinfo(struct sk_buff *skb, const struct net_bridge_port *port,
68 u32 pid, u32 seq, int event, unsigned int flags)
69{
70 const struct net_bridge *br = port->br;
71 const struct net_device *dev = port->dev;
Thomas Graf74685962006-11-20 16:20:22 -080072 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070073 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070074 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070075
stephen hemminger28a16c92010-05-10 09:31:09 +000076 br_debug(br, "br_fill_info event %d port %s master %s\n",
77 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070078
Thomas Graf74685962006-11-20 16:20:22 -080079 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
80 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -080081 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070082
Thomas Graf74685962006-11-20 16:20:22 -080083 hdr = nlmsg_data(nlh);
84 hdr->ifi_family = AF_BRIDGE;
85 hdr->__ifi_pad = 0;
86 hdr->ifi_type = dev->type;
87 hdr->ifi_index = dev->ifindex;
88 hdr->ifi_flags = dev_get_flags(dev);
89 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070090
David S. Miller2eb812e2012-04-01 20:49:54 -040091 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
92 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
93 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
94 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
95 (dev->addr_len &&
96 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
97 (dev->ifindex != dev->iflink &&
stephen hemminger25c71c72012-11-13 07:53:05 +000098 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
David S. Miller2eb812e2012-04-01 20:49:54 -040099 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000100
101 if (event == RTM_NEWLINK) {
102 struct nlattr *nest
103 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
104
105 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
106 goto nla_put_failure;
107 nla_nest_end(skb, nest);
108 }
109
Thomas Graf74685962006-11-20 16:20:22 -0800110 return nlmsg_end(skb, nlh);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700111
Thomas Graf74685962006-11-20 16:20:22 -0800112nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800113 nlmsg_cancel(skb, nlh);
114 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700115}
116
117/*
118 * Notify listeners of a change in port information
119 */
120void br_ifinfo_notify(int event, struct net_bridge_port *port)
121{
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700122 struct net *net = dev_net(port->dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700123 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700124 int err = -ENOBUFS;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700125
stephen hemminger28a16c92010-05-10 09:31:09 +0000126 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000127 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000128
Thomas Graf339bf982006-11-10 14:10:15 -0800129 skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700130 if (skb == NULL)
131 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700132
Thomas Graf280a3062006-08-15 00:36:28 -0700133 err = br_fill_ifinfo(skb, port, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -0800134 if (err < 0) {
135 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
136 WARN_ON(err == -EMSGSIZE);
137 kfree_skb(skb);
138 goto errout;
139 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800140 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
141 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700142errout:
Stephen Hemmingerbea1b422006-08-03 16:24:02 -0700143 if (err < 0)
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700144 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700145}
146
147/*
148 * Dump information about all ports, in response to GETLINK
149 */
John Fastabende5a55a82012-10-24 08:12:57 +0000150int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
151 struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700152{
John Fastabende5a55a82012-10-24 08:12:57 +0000153 int err = 0;
154 struct net_bridge_port *port = br_port_get_rcu(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700155
John Fastabende5a55a82012-10-24 08:12:57 +0000156 /* not a bridge port */
157 if (!port)
158 goto out;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000159
John Fastabende5a55a82012-10-24 08:12:57 +0000160 err = br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, NLM_F_MULTI);
161out:
162 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700163}
164
stephen hemminger25c71c72012-11-13 07:53:05 +0000165static const struct nla_policy ifla_brport_policy[IFLA_BRPORT_MAX + 1] = {
166 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
167 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
168 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
169 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000170 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000171 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000172};
173
174/* Change the state of the port and notify spanning tree */
175static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700176{
stephen hemminger25c71c72012-11-13 07:53:05 +0000177 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000178 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700179
180 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700181 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700182 return -EBUSY;
183
stephen hemminger25c71c72012-11-13 07:53:05 +0000184 if (!netif_running(p->dev) ||
185 (!netif_carrier_ok(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700186 return -ENETDOWN;
187
stephen hemminger25c71c72012-11-13 07:53:05 +0000188 p->state = state;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700189 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000190 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700191 return 0;
192}
193
stephen hemminger25c71c72012-11-13 07:53:05 +0000194/* Set/clear or port flags based on attribute */
195static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
196 int attrtype, unsigned long mask)
197{
198 if (tb[attrtype]) {
199 u8 flag = nla_get_u8(tb[attrtype]);
200 if (flag)
201 p->flags |= mask;
202 else
203 p->flags &= ~mask;
204 }
205}
206
207/* Process bridge protocol info on port */
208static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
209{
210 int err;
211
212 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000213 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500214 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
stephen hemminger25c71c72012-11-13 07:53:05 +0000215
216 if (tb[IFLA_BRPORT_COST]) {
217 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
218 if (err)
219 return err;
220 }
221
222 if (tb[IFLA_BRPORT_PRIORITY]) {
223 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
224 if (err)
225 return err;
226 }
227
228 if (tb[IFLA_BRPORT_STATE]) {
229 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
230 if (err)
231 return err;
232 }
233 return 0;
234}
235
236/* Change state and parameters on port. */
237int br_setlink(struct net_device *dev, struct nlmsghdr *nlh)
238{
239 struct ifinfomsg *ifm;
240 struct nlattr *protinfo;
241 struct net_bridge_port *p;
242 struct nlattr *tb[IFLA_BRPORT_MAX];
243 int err;
244
245 ifm = nlmsg_data(nlh);
246
247 protinfo = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_PROTINFO);
248 if (!protinfo)
249 return 0;
250
251 p = br_port_get_rtnl(dev);
252 if (!p)
253 return -EINVAL;
254
255 if (protinfo->nla_type & NLA_F_NESTED) {
256 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
257 protinfo, ifla_brport_policy);
258 if (err)
259 return err;
260
261 spin_lock_bh(&p->br->lock);
262 err = br_setport(p, tb);
263 spin_unlock_bh(&p->br->lock);
264 } else {
265 /* Binary compatability with old RSTP */
266 if (nla_len(protinfo) < sizeof(u8))
267 return -EINVAL;
268
269 spin_lock_bh(&p->br->lock);
270 err = br_set_port_state(p, nla_get_u8(protinfo));
271 spin_unlock_bh(&p->br->lock);
272 }
273
274 if (err == 0)
275 br_ifinfo_notify(RTM_NEWLINK, p);
276
277 return err;
278}
279
stephen hemmingerbb900b22011-04-04 14:03:32 +0000280static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700281{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000282 if (tb[IFLA_ADDRESS]) {
283 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
284 return -EINVAL;
285 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
286 return -EADDRNOTAVAIL;
287 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700288
289 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700290}
291
stephen hemminger149ddd82012-06-26 05:48:45 +0000292struct rtnl_link_ops br_link_ops __read_mostly = {
stephen hemmingerbb900b22011-04-04 14:03:32 +0000293 .kind = "bridge",
294 .priv_size = sizeof(struct net_bridge),
295 .setup = br_dev_setup,
296 .validate = br_validate,
stephen hemminger1ce5cce2011-10-06 11:19:41 +0000297 .dellink = br_dev_delete,
stephen hemmingerbb900b22011-04-04 14:03:32 +0000298};
299
300int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700301{
John Fastabende5a55a82012-10-24 08:12:57 +0000302 return rtnl_link_register(&br_link_ops);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700303}
304
stephen hemmingerbb900b22011-04-04 14:03:32 +0000305void __exit br_netlink_fini(void)
306{
307 rtnl_link_unregister(&br_link_ops);
308 rtnl_unregister_all(PF_BRIDGE);
309}