blob: c720839687688184b361d9c0b3cb6d1a1c586412 [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 Prabhufed0a152015-02-25 23:55:40 -080025static int br_get_num_vlan_infos(const struct net_port_vlans *pv,
26 u32 filter_mask)
27{
28 u16 vid_range_start = 0, vid_range_end = 0;
29 u16 vid_range_flags = 0;
30 u16 pvid, vid, flags;
31 int num_vlans = 0;
32
33 if (filter_mask & RTEXT_FILTER_BRVLAN)
34 return pv->num_vlans;
35
36 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
37 return 0;
38
39 /* Count number of vlan info's
40 */
41 pvid = br_get_pvid(pv);
42 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
43 flags = 0;
44 if (vid == pvid)
45 flags |= BRIDGE_VLAN_INFO_PVID;
46
47 if (test_bit(vid, pv->untagged_bitmap))
48 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
49
50 if (vid_range_start == 0) {
51 goto initvars;
52 } else if ((vid - vid_range_end) == 1 &&
53 flags == vid_range_flags) {
54 vid_range_end = vid;
55 continue;
56 } else {
57 if ((vid_range_end - vid_range_start) > 0)
58 num_vlans += 2;
59 else
60 num_vlans += 1;
61 }
62initvars:
63 vid_range_start = vid;
64 vid_range_end = vid;
65 vid_range_flags = flags;
66 }
67
68 if (vid_range_start != 0) {
69 if ((vid_range_end - vid_range_start) > 0)
70 num_vlans += 2;
71 else
72 num_vlans += 1;
73 }
74
75 return num_vlans;
76}
77
78static size_t br_get_link_af_size_filtered(const struct net_device *dev,
79 u32 filter_mask)
Roopa Prabhub7853d72015-02-21 20:21:51 -080080{
81 struct net_port_vlans *pv;
Roopa Prabhufed0a152015-02-25 23:55:40 -080082 int num_vlan_infos;
Roopa Prabhub7853d72015-02-21 20:21:51 -080083
Johannes Berg2f56f6b2015-03-03 16:02:16 +010084 rcu_read_lock();
Roopa Prabhub7853d72015-02-21 20:21:51 -080085 if (br_port_exists(dev))
Johannes Berg2f56f6b2015-03-03 16:02:16 +010086 pv = nbp_get_vlan_info(br_port_get_rcu(dev));
Roopa Prabhub7853d72015-02-21 20:21:51 -080087 else if (dev->priv_flags & IFF_EBRIDGE)
88 pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
89 else
Johannes Berg2f56f6b2015-03-03 16:02:16 +010090 pv = NULL;
91 if (pv)
92 num_vlan_infos = br_get_num_vlan_infos(pv, filter_mask);
93 else
94 num_vlan_infos = 0;
95 rcu_read_unlock();
Roopa Prabhub7853d72015-02-21 20:21:51 -080096
Roopa Prabhufed0a152015-02-25 23:55:40 -080097 if (!num_vlan_infos)
98 return 0;
99
Roopa Prabhub7853d72015-02-21 20:21:51 -0800100 /* Each VLAN is returned in bridge_vlan_info along with flags */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800101 return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhub7853d72015-02-21 20:21:51 -0800102}
103
stephen hemminger25c71c72012-11-13 07:53:05 +0000104static inline size_t br_port_info_size(void)
105{
106 return nla_total_size(1) /* IFLA_BRPORT_STATE */
107 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
108 + nla_total_size(4) /* IFLA_BRPORT_COST */
109 + nla_total_size(1) /* IFLA_BRPORT_MODE */
stephen hemmingera2e01a62012-11-13 07:53:07 +0000110 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
stephen hemminger1007dd12012-11-13 07:53:08 +0000111 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
stephen hemminger3da889b2013-03-11 13:52:17 +0000112 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400113 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400114 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
stephen hemminger25c71c72012-11-13 07:53:05 +0000115 + 0;
116}
117
Roopa Prabhufed0a152015-02-25 23:55:40 -0800118static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800119{
120 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000121 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
122 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
123 + nla_total_size(4) /* IFLA_MASTER */
124 + nla_total_size(4) /* IFLA_MTU */
125 + nla_total_size(4) /* IFLA_LINK */
126 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800127 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800128 + nla_total_size(br_get_link_af_size_filtered(dev,
129 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000130}
131
132static int br_port_fill_attrs(struct sk_buff *skb,
133 const struct net_bridge_port *p)
134{
135 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
136
137 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
138 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
139 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000140 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000141 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -0500142 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400143 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400144 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Kyeyoon Park95850112014-10-23 14:49:17 -0700145 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
146 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)))
stephen hemminger25c71c72012-11-13 07:53:05 +0000147 return -EMSGSIZE;
148
149 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800150}
151
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800152static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
153 u16 vid_end, u16 flags)
154{
155 struct bridge_vlan_info vinfo;
156
157 if ((vid_end - vid_start) > 0) {
158 /* add range to skb */
159 vinfo.vid = vid_start;
160 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
161 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
162 sizeof(vinfo), &vinfo))
163 goto nla_put_failure;
164
165 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
166
167 vinfo.vid = vid_end;
168 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
169 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
170 sizeof(vinfo), &vinfo))
171 goto nla_put_failure;
172 } else {
173 vinfo.vid = vid_start;
174 vinfo.flags = flags;
175 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
176 sizeof(vinfo), &vinfo))
177 goto nla_put_failure;
178 }
179
180 return 0;
181
182nla_put_failure:
183 return -EMSGSIZE;
184}
185
186static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
187 const struct net_port_vlans *pv)
188{
189 u16 vid_range_start = 0, vid_range_end = 0;
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800190 u16 vid_range_flags = 0;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800191 u16 pvid, vid, flags;
192 int err = 0;
193
194 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
195 * and mark vlan info with begin and end flags
196 * if vlaninfo represents a range
197 */
198 pvid = br_get_pvid(pv);
199 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
200 flags = 0;
201 if (vid == pvid)
202 flags |= BRIDGE_VLAN_INFO_PVID;
203
204 if (test_bit(vid, pv->untagged_bitmap))
205 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
206
207 if (vid_range_start == 0) {
208 goto initvars;
209 } else if ((vid - vid_range_end) == 1 &&
210 flags == vid_range_flags) {
211 vid_range_end = vid;
212 continue;
213 } else {
214 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
215 vid_range_end,
216 vid_range_flags);
217 if (err)
218 return err;
219 }
220
221initvars:
222 vid_range_start = vid;
223 vid_range_end = vid;
224 vid_range_flags = flags;
225 }
226
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800227 if (vid_range_start != 0) {
228 /* Call it once more to send any left over vlans */
229 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
230 vid_range_end,
231 vid_range_flags);
232 if (err)
233 return err;
234 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800235
236 return 0;
237}
238
239static int br_fill_ifvlaninfo(struct sk_buff *skb,
240 const struct net_port_vlans *pv)
241{
242 struct bridge_vlan_info vinfo;
243 u16 pvid, vid;
244
245 pvid = br_get_pvid(pv);
246 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
247 vinfo.vid = vid;
248 vinfo.flags = 0;
249 if (vid == pvid)
250 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
251
252 if (test_bit(vid, pv->untagged_bitmap))
253 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
254
255 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
256 sizeof(vinfo), &vinfo))
257 goto nla_put_failure;
258 }
259
260 return 0;
261
262nla_put_failure:
263 return -EMSGSIZE;
264}
265
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700266/*
267 * Create one netlink message for one interface
268 * Contains port and master info as well as carrier and bridge state.
269 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000270static int br_fill_ifinfo(struct sk_buff *skb,
271 const struct net_bridge_port *port,
272 u32 pid, u32 seq, int event, unsigned int flags,
273 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700274{
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000275 const struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800276 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700277 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700278 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700279
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000280 if (port)
281 br = port->br;
282 else
283 br = netdev_priv(dev);
284
stephen hemminger28a16c92010-05-10 09:31:09 +0000285 br_debug(br, "br_fill_info event %d port %s master %s\n",
286 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700287
Thomas Graf74685962006-11-20 16:20:22 -0800288 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
289 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800290 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700291
Thomas Graf74685962006-11-20 16:20:22 -0800292 hdr = nlmsg_data(nlh);
293 hdr->ifi_family = AF_BRIDGE;
294 hdr->__ifi_pad = 0;
295 hdr->ifi_type = dev->type;
296 hdr->ifi_index = dev->ifindex;
297 hdr->ifi_flags = dev_get_flags(dev);
298 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700299
David S. Miller2eb812e2012-04-01 20:49:54 -0400300 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
301 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
302 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
303 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
304 (dev->addr_len &&
305 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
306 (dev->ifindex != dev->iflink &&
stephen hemminger25c71c72012-11-13 07:53:05 +0000307 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
David S. Miller2eb812e2012-04-01 20:49:54 -0400308 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000309
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000310 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000311 struct nlattr *nest
312 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
313
314 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
315 goto nla_put_failure;
316 nla_nest_end(skb, nest);
317 }
318
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000319 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800320 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
321 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000322 const struct net_port_vlans *pv;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800323 struct nlattr *af;
324 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000325
326 if (port)
327 pv = nbp_get_vlan_info(port);
328 else
329 pv = br_get_vlan_info(br);
330
Toshiaki Makitaef40b7e2013-08-20 17:10:18 +0900331 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID))
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000332 goto done;
333
334 af = nla_nest_start(skb, IFLA_AF_SPEC);
335 if (!af)
336 goto nla_put_failure;
337
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800338 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
339 err = br_fill_ifvlaninfo_compressed(skb, pv);
340 else
341 err = br_fill_ifvlaninfo(skb, pv);
342 if (err)
343 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000344 nla_nest_end(skb, af);
345 }
346
347done:
Johannes Berg053c0952015-01-16 22:09:00 +0100348 nlmsg_end(skb, nlh);
349 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700350
Thomas Graf74685962006-11-20 16:20:22 -0800351nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800352 nlmsg_cancel(skb, nlh);
353 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700354}
355
356/*
357 * Notify listeners of a change in port information
358 */
359void br_ifinfo_notify(int event, struct net_bridge_port *port)
360{
Vlad Yasevich407af322013-02-13 12:00:12 +0000361 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700362 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700363 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800364 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700365
Vlad Yasevich407af322013-02-13 12:00:12 +0000366 if (!port)
367 return;
368
369 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000370 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000371 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000372
Roopa Prabhufed0a152015-02-25 23:55:40 -0800373 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700374 if (skb == NULL)
375 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700376
Roopa Prabhufed0a152015-02-25 23:55:40 -0800377 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800378 if (err < 0) {
379 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
380 WARN_ON(err == -EMSGSIZE);
381 kfree_skb(skb);
382 goto errout;
383 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800384 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
385 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700386errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800387 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700388}
389
Vlad Yasevich407af322013-02-13 12:00:12 +0000390
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700391/*
392 * Dump information about all ports, in response to GETLINK
393 */
John Fastabende5a55a82012-10-24 08:12:57 +0000394int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000395 struct net_device *dev, u32 filter_mask)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700396{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800397 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700398
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800399 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
400 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300401 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000402
Dan Carpenter1b846f92015-01-21 12:22:35 +0300403 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, NLM_F_MULTI,
404 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700405}
406
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800407static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
408 int cmd, struct bridge_vlan_info *vinfo)
409{
410 int err = 0;
411
412 switch (cmd) {
413 case RTM_SETLINK:
414 if (p) {
415 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
416 if (err)
417 break;
418
419 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
420 err = br_vlan_add(p->br, vinfo->vid,
421 vinfo->flags);
422 } else {
423 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
424 }
425 break;
426
427 case RTM_DELLINK:
428 if (p) {
429 nbp_vlan_delete(p, vinfo->vid);
430 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
431 br_vlan_delete(p->br, vinfo->vid);
432 } else {
433 br_vlan_delete(br, vinfo->vid);
434 }
435 break;
436 }
437
438 return err;
439}
Vlad Yasevich407af322013-02-13 12:00:12 +0000440
441static int br_afspec(struct net_bridge *br,
442 struct net_bridge_port *p,
443 struct nlattr *af_spec,
444 int cmd)
445{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800446 struct bridge_vlan_info *vinfo_start = NULL;
447 struct bridge_vlan_info *vinfo = NULL;
448 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000449 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800450 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000451
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800452 nla_for_each_nested(attr, af_spec, rem) {
453 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
454 continue;
455 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000456 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800457 vinfo = nla_data(attr);
458 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
459 if (vinfo_start)
460 return -EINVAL;
461 vinfo_start = vinfo;
462 continue;
463 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000464
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800465 if (vinfo_start) {
466 struct bridge_vlan_info tmp_vinfo;
467 int v;
468
469 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
470 return -EINVAL;
471
472 if (vinfo->vid <= vinfo_start->vid)
473 return -EINVAL;
474
475 memcpy(&tmp_vinfo, vinfo_start,
476 sizeof(struct bridge_vlan_info));
477
478 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
479 tmp_vinfo.vid = v;
480 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000481 if (err)
482 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800483 }
484 vinfo_start = NULL;
485 } else {
486 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000487 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800488 if (err)
489 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000490 }
491
492 return err;
493}
494
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200495static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000496 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
497 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
498 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
499 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000500 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000501 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100502 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400503 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400504 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000505};
506
507/* Change the state of the port and notify spanning tree */
508static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700509{
stephen hemminger25c71c72012-11-13 07:53:05 +0000510 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000511 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700512
513 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700514 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700515 return -EBUSY;
516
stephen hemminger576eb622012-12-28 18:15:22 +0000517 /* if device is not up, change is not allowed
518 * if link is not present, only allowable state is disabled
519 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000520 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000521 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700522 return -ENETDOWN;
523
Florian Fainelli775dd692014-09-30 16:13:19 -0700524 br_set_state(p, state);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700525 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000526 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700527 return 0;
528}
529
stephen hemminger25c71c72012-11-13 07:53:05 +0000530/* Set/clear or port flags based on attribute */
531static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
532 int attrtype, unsigned long mask)
533{
534 if (tb[attrtype]) {
535 u8 flag = nla_get_u8(tb[attrtype]);
536 if (flag)
537 p->flags |= mask;
538 else
539 p->flags &= ~mask;
540 }
541}
542
543/* Process bridge protocol info on port */
544static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
545{
546 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400547 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000548
549 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000550 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500551 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000552 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400553 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400554 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Kyeyoon Park95850112014-10-23 14:49:17 -0700555 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
stephen hemminger25c71c72012-11-13 07:53:05 +0000556
557 if (tb[IFLA_BRPORT_COST]) {
558 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
559 if (err)
560 return err;
561 }
562
563 if (tb[IFLA_BRPORT_PRIORITY]) {
564 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
565 if (err)
566 return err;
567 }
568
569 if (tb[IFLA_BRPORT_STATE]) {
570 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
571 if (err)
572 return err;
573 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400574
575 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000576 return 0;
577}
578
579/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800580int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000581{
stephen hemminger25c71c72012-11-13 07:53:05 +0000582 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000583 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000584 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000585 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Roopa Prabhu68e331c2015-01-29 22:40:14 -0800586 int err = 0, ret_offload = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000587
Hong zhi guoc60ee672013-03-28 06:21:22 +0000588 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
589 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000590 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000591 return 0;
592
593 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000594 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800595 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000596 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000597 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000598 return -EINVAL;
599
Vlad Yasevich407af322013-02-13 12:00:12 +0000600 if (p && protinfo) {
601 if (protinfo->nla_type & NLA_F_NESTED) {
602 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200603 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000604 if (err)
605 return err;
606
607 spin_lock_bh(&p->br->lock);
608 err = br_setport(p, tb);
609 spin_unlock_bh(&p->br->lock);
610 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800611 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000612 if (nla_len(protinfo) < sizeof(u8))
613 return -EINVAL;
614
615 spin_lock_bh(&p->br->lock);
616 err = br_set_port_state(p, nla_get_u8(protinfo));
617 spin_unlock_bh(&p->br->lock);
618 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000619 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000620 goto out;
621 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000622
Vlad Yasevich407af322013-02-13 12:00:12 +0000623 if (afspec) {
624 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
625 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000626 }
627
Roopa Prabhu1fd0bdd2015-02-05 22:24:45 -0800628 if (p && !(flags & BRIDGE_FLAGS_SELF)) {
Roopa Prabhu68e331c2015-01-29 22:40:14 -0800629 /* set bridge attributes in hardware if supported
630 */
631 ret_offload = netdev_switch_port_bridge_setlink(dev, nlh,
632 flags);
633 if (ret_offload && ret_offload != -EOPNOTSUPP)
634 br_warn(p->br, "error setting attrs on port %u(%s)\n",
635 (unsigned int)p->port_no, p->dev->name);
636 }
637
stephen hemminger25c71c72012-11-13 07:53:05 +0000638 if (err == 0)
639 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000640out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000641 return err;
642}
643
Vlad Yasevich407af322013-02-13 12:00:12 +0000644/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800645int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000646{
Vlad Yasevich407af322013-02-13 12:00:12 +0000647 struct nlattr *afspec;
648 struct net_bridge_port *p;
Roopa Prabhu68e331c2015-01-29 22:40:14 -0800649 int err = 0, ret_offload = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000650
Hong zhi guoc60ee672013-03-28 06:21:22 +0000651 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000652 if (!afspec)
653 return 0;
654
655 p = br_port_get_rtnl(dev);
656 /* We want to accept dev as bridge itself as well */
657 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
658 return -EINVAL;
659
660 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
661 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800662 if (err == 0)
663 /* Send RTM_NEWLINK because userspace
664 * expects RTM_NEWLINK for vlan dels
665 */
666 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000667
Roopa Prabhu1fd0bdd2015-02-05 22:24:45 -0800668 if (p && !(flags & BRIDGE_FLAGS_SELF)) {
Roopa Prabhu68e331c2015-01-29 22:40:14 -0800669 /* del bridge attributes in hardware
670 */
671 ret_offload = netdev_switch_port_bridge_dellink(dev, nlh,
672 flags);
673 if (ret_offload && ret_offload != -EOPNOTSUPP)
674 br_warn(p->br, "error deleting attrs on port %u (%s)\n",
675 (unsigned int)p->port_no, p->dev->name);
676 }
677
Vlad Yasevich407af322013-02-13 12:00:12 +0000678 return err;
679}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000680static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700681{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000682 if (tb[IFLA_ADDRESS]) {
683 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
684 return -EINVAL;
685 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
686 return -EADDRNOTAVAIL;
687 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700688
689 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700690}
691
Toshiaki Makita30313a32014-04-25 17:01:18 +0900692static int br_dev_newlink(struct net *src_net, struct net_device *dev,
693 struct nlattr *tb[], struct nlattr *data[])
694{
695 struct net_bridge *br = netdev_priv(dev);
696
697 if (tb[IFLA_ADDRESS]) {
698 spin_lock_bh(&br->lock);
699 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
700 spin_unlock_bh(&br->lock);
701 }
702
703 return register_netdevice(dev);
704}
705
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200706static int br_port_slave_changelink(struct net_device *brdev,
707 struct net_device *dev,
708 struct nlattr *tb[],
709 struct nlattr *data[])
710{
711 if (!data)
712 return 0;
713 return br_setport(br_port_get_rtnl(dev), data);
714}
715
Jiri Pirkoced82832014-09-05 15:51:29 +0200716static int br_port_fill_slave_info(struct sk_buff *skb,
717 const struct net_device *brdev,
718 const struct net_device *dev)
719{
720 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
721}
722
723static size_t br_port_get_slave_size(const struct net_device *brdev,
724 const struct net_device *dev)
725{
726 return br_port_info_size();
727}
728
Jiri Pirko13323512014-09-05 15:51:32 +0200729static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
730 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
731 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
732 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
733};
734
735static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
736 struct nlattr *data[])
737{
738 struct net_bridge *br = netdev_priv(brdev);
739 int err;
740
741 if (!data)
742 return 0;
743
744 if (data[IFLA_BR_FORWARD_DELAY]) {
745 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
746 if (err)
747 return err;
748 }
749
750 if (data[IFLA_BR_HELLO_TIME]) {
751 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
752 if (err)
753 return err;
754 }
755
756 if (data[IFLA_BR_MAX_AGE]) {
757 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
758 if (err)
759 return err;
760 }
761
762 return 0;
763}
764
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200765static size_t br_get_size(const struct net_device *brdev)
766{
767 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
768 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
769 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
770 0;
771}
772
773static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
774{
775 struct net_bridge *br = netdev_priv(brdev);
776 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
777 u32 hello_time = jiffies_to_clock_t(br->hello_time);
778 u32 age_time = jiffies_to_clock_t(br->max_age);
779
780 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
781 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
782 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time))
783 return -EMSGSIZE;
784
785 return 0;
786}
787
Roopa Prabhufed0a152015-02-25 23:55:40 -0800788static size_t br_get_link_af_size(const struct net_device *dev)
789{
790 struct net_port_vlans *pv;
791
792 if (br_port_exists(dev))
793 pv = nbp_get_vlan_info(br_port_get_rtnl(dev));
794 else if (dev->priv_flags & IFF_EBRIDGE)
795 pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
796 else
797 return 0;
798
799 if (!pv)
800 return 0;
801
802 /* Each VLAN is returned in bridge_vlan_info along with flags */
803 return pv->num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
804}
805
Daniel Borkmann207895fd32015-01-29 12:15:03 +0100806static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000807 .family = AF_BRIDGE,
808 .get_link_af_size = br_get_link_af_size,
809};
810
stephen hemminger149ddd82012-06-26 05:48:45 +0000811struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +0200812 .kind = "bridge",
813 .priv_size = sizeof(struct net_bridge),
814 .setup = br_dev_setup,
Jiri Pirko13323512014-09-05 15:51:32 +0200815 .maxtype = IFLA_BRPORT_MAX,
816 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +0200817 .validate = br_validate,
818 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +0200819 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +0200820 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200821 .get_size = br_get_size,
822 .fill_info = br_fill_info,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200823
824 .slave_maxtype = IFLA_BRPORT_MAX,
825 .slave_policy = br_port_policy,
826 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +0200827 .get_slave_size = br_port_get_slave_size,
828 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +0000829};
830
831int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700832{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000833 int err;
834
835 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800836 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000837
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000838 err = rtnl_link_register(&br_link_ops);
839 if (err)
840 goto out_af;
841
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000842 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000843
844out_af:
845 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000846 br_mdb_uninit();
847 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700848}
849
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +0200850void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +0000851{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000852 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000853 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +0000854 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +0000855}