blob: 17e0177467f583524fed1c37c18f79cbdd02c768 [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>
Roopa Prabhu68e331c2015-01-29 22:40:14 -080019#include <net/switchdev.h>
Vlad Yasevich407af322013-02-13 12:00:12 +000020#include <uapi/linux/if_bridge.h>
stephen hemmingerbb900b22011-04-04 14:03:32 +000021
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070022#include "br_private.h"
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +000023#include "br_private_stp.h"
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070024
Roopa Prabhub7853d72015-02-21 20:21:51 -080025static size_t br_get_link_af_size(const struct net_device *dev)
26{
27 struct net_port_vlans *pv;
28
29 if (br_port_exists(dev))
30 pv = nbp_get_vlan_info(br_port_get_rtnl(dev));
31 else if (dev->priv_flags & IFF_EBRIDGE)
32 pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
33 else
34 return 0;
35
36 if (!pv)
37 return 0;
38
39 /* Each VLAN is returned in bridge_vlan_info along with flags */
40 return pv->num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
41}
42
stephen hemminger25c71c72012-11-13 07:53:05 +000043static inline size_t br_port_info_size(void)
44{
45 return nla_total_size(1) /* IFLA_BRPORT_STATE */
46 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
47 + nla_total_size(4) /* IFLA_BRPORT_COST */
48 + nla_total_size(1) /* IFLA_BRPORT_MODE */
stephen hemmingera2e01a62012-11-13 07:53:07 +000049 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
stephen hemminger1007dd12012-11-13 07:53:08 +000050 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
stephen hemminger3da889b2013-03-11 13:52:17 +000051 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
Vlad Yasevich9ba18892013-06-05 10:08:00 -040052 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
Vlad Yasevich867a5942013-06-05 10:08:01 -040053 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
stephen hemminger25c71c72012-11-13 07:53:05 +000054 + 0;
55}
56
Roopa Prabhub7853d72015-02-21 20:21:51 -080057static inline size_t br_nlmsg_size(struct net_device *dev)
Thomas Graf339bf982006-11-10 14:10:15 -080058{
59 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +000060 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
61 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
62 + nla_total_size(4) /* IFLA_MASTER */
63 + nla_total_size(4) /* IFLA_MTU */
64 + nla_total_size(4) /* IFLA_LINK */
65 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -080066 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
67 + nla_total_size(br_get_link_af_size(dev)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +000068}
69
70static int br_port_fill_attrs(struct sk_buff *skb,
71 const struct net_bridge_port *p)
72{
73 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
74
75 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
76 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
77 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +000078 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +000079 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -050080 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
Vlad Yasevich9ba18892013-06-05 10:08:00 -040081 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -040082 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Kyeyoon Park95850112014-10-23 14:49:17 -070083 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
84 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)))
stephen hemminger25c71c72012-11-13 07:53:05 +000085 return -EMSGSIZE;
86
87 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -080088}
89
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -080090static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
91 u16 vid_end, u16 flags)
92{
93 struct bridge_vlan_info vinfo;
94
95 if ((vid_end - vid_start) > 0) {
96 /* add range to skb */
97 vinfo.vid = vid_start;
98 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
99 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
100 sizeof(vinfo), &vinfo))
101 goto nla_put_failure;
102
103 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
104
105 vinfo.vid = vid_end;
106 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
107 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
108 sizeof(vinfo), &vinfo))
109 goto nla_put_failure;
110 } else {
111 vinfo.vid = vid_start;
112 vinfo.flags = flags;
113 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
114 sizeof(vinfo), &vinfo))
115 goto nla_put_failure;
116 }
117
118 return 0;
119
120nla_put_failure:
121 return -EMSGSIZE;
122}
123
124static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
125 const struct net_port_vlans *pv)
126{
127 u16 vid_range_start = 0, vid_range_end = 0;
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800128 u16 vid_range_flags = 0;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800129 u16 pvid, vid, flags;
130 int err = 0;
131
132 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
133 * and mark vlan info with begin and end flags
134 * if vlaninfo represents a range
135 */
136 pvid = br_get_pvid(pv);
137 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
138 flags = 0;
139 if (vid == pvid)
140 flags |= BRIDGE_VLAN_INFO_PVID;
141
142 if (test_bit(vid, pv->untagged_bitmap))
143 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
144
145 if (vid_range_start == 0) {
146 goto initvars;
147 } else if ((vid - vid_range_end) == 1 &&
148 flags == vid_range_flags) {
149 vid_range_end = vid;
150 continue;
151 } else {
152 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
153 vid_range_end,
154 vid_range_flags);
155 if (err)
156 return err;
157 }
158
159initvars:
160 vid_range_start = vid;
161 vid_range_end = vid;
162 vid_range_flags = flags;
163 }
164
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800165 if (vid_range_start != 0) {
166 /* Call it once more to send any left over vlans */
167 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
168 vid_range_end,
169 vid_range_flags);
170 if (err)
171 return err;
172 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800173
174 return 0;
175}
176
177static int br_fill_ifvlaninfo(struct sk_buff *skb,
178 const struct net_port_vlans *pv)
179{
180 struct bridge_vlan_info vinfo;
181 u16 pvid, vid;
182
183 pvid = br_get_pvid(pv);
184 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
185 vinfo.vid = vid;
186 vinfo.flags = 0;
187 if (vid == pvid)
188 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
189
190 if (test_bit(vid, pv->untagged_bitmap))
191 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
192
193 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
194 sizeof(vinfo), &vinfo))
195 goto nla_put_failure;
196 }
197
198 return 0;
199
200nla_put_failure:
201 return -EMSGSIZE;
202}
203
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700204/*
205 * Create one netlink message for one interface
206 * Contains port and master info as well as carrier and bridge state.
207 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000208static int br_fill_ifinfo(struct sk_buff *skb,
209 const struct net_bridge_port *port,
210 u32 pid, u32 seq, int event, unsigned int flags,
211 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700212{
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000213 const struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800214 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700215 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700216 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700217
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000218 if (port)
219 br = port->br;
220 else
221 br = netdev_priv(dev);
222
stephen hemminger28a16c92010-05-10 09:31:09 +0000223 br_debug(br, "br_fill_info event %d port %s master %s\n",
224 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700225
Thomas Graf74685962006-11-20 16:20:22 -0800226 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
227 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800228 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700229
Thomas Graf74685962006-11-20 16:20:22 -0800230 hdr = nlmsg_data(nlh);
231 hdr->ifi_family = AF_BRIDGE;
232 hdr->__ifi_pad = 0;
233 hdr->ifi_type = dev->type;
234 hdr->ifi_index = dev->ifindex;
235 hdr->ifi_flags = dev_get_flags(dev);
236 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700237
David S. Miller2eb812e2012-04-01 20:49:54 -0400238 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
239 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
240 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
241 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
242 (dev->addr_len &&
243 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
244 (dev->ifindex != dev->iflink &&
stephen hemminger25c71c72012-11-13 07:53:05 +0000245 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
David S. Miller2eb812e2012-04-01 20:49:54 -0400246 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000247
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000248 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000249 struct nlattr *nest
250 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
251
252 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
253 goto nla_put_failure;
254 nla_nest_end(skb, nest);
255 }
256
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000257 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800258 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
259 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000260 const struct net_port_vlans *pv;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800261 struct nlattr *af;
262 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000263
264 if (port)
265 pv = nbp_get_vlan_info(port);
266 else
267 pv = br_get_vlan_info(br);
268
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900269 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID))
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000270 goto done;
271
272 af = nla_nest_start(skb, IFLA_AF_SPEC);
273 if (!af)
274 goto nla_put_failure;
275
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800276 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
277 err = br_fill_ifvlaninfo_compressed(skb, pv);
278 else
279 err = br_fill_ifvlaninfo(skb, pv);
280 if (err)
281 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000282 nla_nest_end(skb, af);
283 }
284
285done:
Johannes Berg053c0952015-01-16 22:09:00 +0100286 nlmsg_end(skb, nlh);
287 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700288
Thomas Graf74685962006-11-20 16:20:22 -0800289nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800290 nlmsg_cancel(skb, nlh);
291 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700292}
293
294/*
295 * Notify listeners of a change in port information
296 */
297void br_ifinfo_notify(int event, struct net_bridge_port *port)
298{
Vlad Yasevich407af322013-02-13 12:00:12 +0000299 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700300 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700301 int err = -ENOBUFS;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700302
Vlad Yasevich407af322013-02-13 12:00:12 +0000303 if (!port)
304 return;
305
306 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000307 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000308 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000309
Roopa Prabhub7853d72015-02-21 20:21:51 -0800310 skb = nlmsg_new(br_nlmsg_size(port->dev), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700311 if (skb == NULL)
312 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700313
Roopa Prabhub7853d72015-02-21 20:21:51 -0800314 err = br_fill_ifinfo(skb, port, 0, 0, event, 0,
315 RTEXT_FILTER_BRVLAN_COMPRESSED, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800316 if (err < 0) {
317 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
318 WARN_ON(err == -EMSGSIZE);
319 kfree_skb(skb);
320 goto errout;
321 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800322 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
323 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700324errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800325 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700326}
327
Vlad Yasevich407af322013-02-13 12:00:12 +0000328
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700329/*
330 * Dump information about all ports, in response to GETLINK
331 */
John Fastabende5a55a82012-10-24 08:12:57 +0000332int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000333 struct net_device *dev, u32 filter_mask)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700334{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800335 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700336
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800337 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
338 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300339 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000340
Dan Carpenter1b846f92015-01-21 12:22:35 +0300341 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, NLM_F_MULTI,
342 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700343}
344
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800345static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
346 int cmd, struct bridge_vlan_info *vinfo)
347{
348 int err = 0;
349
350 switch (cmd) {
351 case RTM_SETLINK:
352 if (p) {
353 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
354 if (err)
355 break;
356
357 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
358 err = br_vlan_add(p->br, vinfo->vid,
359 vinfo->flags);
360 } else {
361 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
362 }
363 break;
364
365 case RTM_DELLINK:
366 if (p) {
367 nbp_vlan_delete(p, vinfo->vid);
368 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
369 br_vlan_delete(p->br, vinfo->vid);
370 } else {
371 br_vlan_delete(br, vinfo->vid);
372 }
373 break;
374 }
375
376 return err;
377}
Vlad Yasevich407af322013-02-13 12:00:12 +0000378
379static int br_afspec(struct net_bridge *br,
380 struct net_bridge_port *p,
381 struct nlattr *af_spec,
382 int cmd)
383{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800384 struct bridge_vlan_info *vinfo_start = NULL;
385 struct bridge_vlan_info *vinfo = NULL;
386 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000387 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800388 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000389
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800390 nla_for_each_nested(attr, af_spec, rem) {
391 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
392 continue;
393 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000394 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800395 vinfo = nla_data(attr);
396 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
397 if (vinfo_start)
398 return -EINVAL;
399 vinfo_start = vinfo;
400 continue;
401 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000402
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800403 if (vinfo_start) {
404 struct bridge_vlan_info tmp_vinfo;
405 int v;
406
407 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
408 return -EINVAL;
409
410 if (vinfo->vid <= vinfo_start->vid)
411 return -EINVAL;
412
413 memcpy(&tmp_vinfo, vinfo_start,
414 sizeof(struct bridge_vlan_info));
415
416 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
417 tmp_vinfo.vid = v;
418 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000419 if (err)
420 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800421 }
422 vinfo_start = NULL;
423 } else {
424 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000425 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800426 if (err)
427 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000428 }
429
430 return err;
431}
432
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200433static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000434 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
435 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
436 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
437 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000438 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000439 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100440 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400441 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400442 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000443};
444
445/* Change the state of the port and notify spanning tree */
446static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700447{
stephen hemminger25c71c72012-11-13 07:53:05 +0000448 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000449 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700450
451 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700452 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700453 return -EBUSY;
454
stephen hemminger576eb622012-12-28 18:15:22 +0000455 /* if device is not up, change is not allowed
456 * if link is not present, only allowable state is disabled
457 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000458 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000459 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700460 return -ENETDOWN;
461
Florian Fainelli775dd692014-09-30 16:13:19 -0700462 br_set_state(p, state);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700463 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000464 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700465 return 0;
466}
467
stephen hemminger25c71c72012-11-13 07:53:05 +0000468/* Set/clear or port flags based on attribute */
469static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
470 int attrtype, unsigned long mask)
471{
472 if (tb[attrtype]) {
473 u8 flag = nla_get_u8(tb[attrtype]);
474 if (flag)
475 p->flags |= mask;
476 else
477 p->flags &= ~mask;
478 }
479}
480
481/* Process bridge protocol info on port */
482static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
483{
484 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400485 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000486
487 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000488 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500489 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000490 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400491 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400492 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Kyeyoon Park95850112014-10-23 14:49:17 -0700493 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
stephen hemminger25c71c72012-11-13 07:53:05 +0000494
495 if (tb[IFLA_BRPORT_COST]) {
496 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
497 if (err)
498 return err;
499 }
500
501 if (tb[IFLA_BRPORT_PRIORITY]) {
502 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
503 if (err)
504 return err;
505 }
506
507 if (tb[IFLA_BRPORT_STATE]) {
508 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
509 if (err)
510 return err;
511 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400512
513 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000514 return 0;
515}
516
517/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800518int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000519{
stephen hemminger25c71c72012-11-13 07:53:05 +0000520 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000521 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000522 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000523 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Roopa Prabhu68e331c2015-01-29 22:40:14 -0800524 int err = 0, ret_offload = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000525
Hong zhi guoc60ee672013-03-28 06:21:22 +0000526 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
527 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000528 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000529 return 0;
530
531 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000532 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800533 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000534 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000535 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000536 return -EINVAL;
537
Vlad Yasevich407af322013-02-13 12:00:12 +0000538 if (p && protinfo) {
539 if (protinfo->nla_type & NLA_F_NESTED) {
540 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200541 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000542 if (err)
543 return err;
544
545 spin_lock_bh(&p->br->lock);
546 err = br_setport(p, tb);
547 spin_unlock_bh(&p->br->lock);
548 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800549 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000550 if (nla_len(protinfo) < sizeof(u8))
551 return -EINVAL;
552
553 spin_lock_bh(&p->br->lock);
554 err = br_set_port_state(p, nla_get_u8(protinfo));
555 spin_unlock_bh(&p->br->lock);
556 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000557 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000558 goto out;
559 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000560
Vlad Yasevich407af322013-02-13 12:00:12 +0000561 if (afspec) {
562 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
563 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000564 }
565
Roopa Prabhu1fd0bdd2015-02-05 22:24:45 -0800566 if (p && !(flags & BRIDGE_FLAGS_SELF)) {
Roopa Prabhu68e331c2015-01-29 22:40:14 -0800567 /* set bridge attributes in hardware if supported
568 */
569 ret_offload = netdev_switch_port_bridge_setlink(dev, nlh,
570 flags);
571 if (ret_offload && ret_offload != -EOPNOTSUPP)
572 br_warn(p->br, "error setting attrs on port %u(%s)\n",
573 (unsigned int)p->port_no, p->dev->name);
574 }
575
stephen hemminger25c71c72012-11-13 07:53:05 +0000576 if (err == 0)
577 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000578out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000579 return err;
580}
581
Vlad Yasevich407af322013-02-13 12:00:12 +0000582/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800583int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000584{
Vlad Yasevich407af322013-02-13 12:00:12 +0000585 struct nlattr *afspec;
586 struct net_bridge_port *p;
Roopa Prabhu68e331c2015-01-29 22:40:14 -0800587 int err = 0, ret_offload = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000588
Hong zhi guoc60ee672013-03-28 06:21:22 +0000589 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000590 if (!afspec)
591 return 0;
592
593 p = br_port_get_rtnl(dev);
594 /* We want to accept dev as bridge itself as well */
595 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
596 return -EINVAL;
597
598 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
599 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800600 if (err == 0)
601 /* Send RTM_NEWLINK because userspace
602 * expects RTM_NEWLINK for vlan dels
603 */
604 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000605
Roopa Prabhu1fd0bdd2015-02-05 22:24:45 -0800606 if (p && !(flags & BRIDGE_FLAGS_SELF)) {
Roopa Prabhu68e331c2015-01-29 22:40:14 -0800607 /* del bridge attributes in hardware
608 */
609 ret_offload = netdev_switch_port_bridge_dellink(dev, nlh,
610 flags);
611 if (ret_offload && ret_offload != -EOPNOTSUPP)
612 br_warn(p->br, "error deleting attrs on port %u (%s)\n",
613 (unsigned int)p->port_no, p->dev->name);
614 }
615
Vlad Yasevich407af322013-02-13 12:00:12 +0000616 return err;
617}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000618static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700619{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000620 if (tb[IFLA_ADDRESS]) {
621 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
622 return -EINVAL;
623 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
624 return -EADDRNOTAVAIL;
625 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700626
627 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700628}
629
Toshiaki Makita30313a32014-04-25 17:01:18 +0900630static int br_dev_newlink(struct net *src_net, struct net_device *dev,
631 struct nlattr *tb[], struct nlattr *data[])
632{
633 struct net_bridge *br = netdev_priv(dev);
634
635 if (tb[IFLA_ADDRESS]) {
636 spin_lock_bh(&br->lock);
637 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
638 spin_unlock_bh(&br->lock);
639 }
640
641 return register_netdevice(dev);
642}
643
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200644static int br_port_slave_changelink(struct net_device *brdev,
645 struct net_device *dev,
646 struct nlattr *tb[],
647 struct nlattr *data[])
648{
649 if (!data)
650 return 0;
651 return br_setport(br_port_get_rtnl(dev), data);
652}
653
Jiri Pirkoced82832014-09-05 15:51:29 +0200654static int br_port_fill_slave_info(struct sk_buff *skb,
655 const struct net_device *brdev,
656 const struct net_device *dev)
657{
658 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
659}
660
661static size_t br_port_get_slave_size(const struct net_device *brdev,
662 const struct net_device *dev)
663{
664 return br_port_info_size();
665}
666
Jiri Pirko13323512014-09-05 15:51:32 +0200667static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
668 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
669 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
670 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
671};
672
673static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
674 struct nlattr *data[])
675{
676 struct net_bridge *br = netdev_priv(brdev);
677 int err;
678
679 if (!data)
680 return 0;
681
682 if (data[IFLA_BR_FORWARD_DELAY]) {
683 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
684 if (err)
685 return err;
686 }
687
688 if (data[IFLA_BR_HELLO_TIME]) {
689 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
690 if (err)
691 return err;
692 }
693
694 if (data[IFLA_BR_MAX_AGE]) {
695 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
696 if (err)
697 return err;
698 }
699
700 return 0;
701}
702
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200703static size_t br_get_size(const struct net_device *brdev)
704{
705 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
706 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
707 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
708 0;
709}
710
711static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
712{
713 struct net_bridge *br = netdev_priv(brdev);
714 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
715 u32 hello_time = jiffies_to_clock_t(br->hello_time);
716 u32 age_time = jiffies_to_clock_t(br->max_age);
717
718 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
719 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
720 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time))
721 return -EMSGSIZE;
722
723 return 0;
724}
725
Daniel Borkmann207895f2015-01-29 12:15:03 +0100726static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000727 .family = AF_BRIDGE,
728 .get_link_af_size = br_get_link_af_size,
729};
730
stephen hemminger149ddd82012-06-26 05:48:45 +0000731struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +0200732 .kind = "bridge",
733 .priv_size = sizeof(struct net_bridge),
734 .setup = br_dev_setup,
Jiri Pirko13323512014-09-05 15:51:32 +0200735 .maxtype = IFLA_BRPORT_MAX,
736 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +0200737 .validate = br_validate,
738 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +0200739 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +0200740 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200741 .get_size = br_get_size,
742 .fill_info = br_fill_info,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200743
744 .slave_maxtype = IFLA_BRPORT_MAX,
745 .slave_policy = br_port_policy,
746 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +0200747 .get_slave_size = br_port_get_slave_size,
748 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +0000749};
750
751int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700752{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000753 int err;
754
755 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800756 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000757
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000758 err = rtnl_link_register(&br_link_ops);
759 if (err)
760 goto out_af;
761
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000762 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000763
764out_af:
765 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000766 br_mdb_uninit();
767 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700768}
769
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +0200770void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +0000771{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000772 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000773 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +0000774 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +0000775}