blob: c64dcad1166254db13fa3074747e8753333fc26a [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>
Vlad Yasevich407af322013-02-13 12:00:12 +000019#include <uapi/linux/if_bridge.h>
stephen hemmingerbb900b22011-04-04 14:03:32 +000020
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070021#include "br_private.h"
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +000022#include "br_private_stp.h"
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070023
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020024static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020025 u32 filter_mask)
Roopa Prabhufed0a152015-02-25 23:55:40 -080026{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020027 struct net_bridge_vlan *v;
28 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020029 u16 flags, pvid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080030 int num_vlans = 0;
31
Roopa Prabhufed0a152015-02-25 23:55:40 -080032 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
33 return 0;
34
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020035 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020036 /* Count number of vlan infos */
37 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhufed0a152015-02-25 23:55:40 -080038 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020039 /* only a context, bridge vlan not activated */
40 if (!br_vlan_should_use(v))
41 continue;
42 if (v->vid == pvid)
Roopa Prabhufed0a152015-02-25 23:55:40 -080043 flags |= BRIDGE_VLAN_INFO_PVID;
44
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020045 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhufed0a152015-02-25 23:55:40 -080046 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
47
48 if (vid_range_start == 0) {
49 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020050 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhufed0a152015-02-25 23:55:40 -080051 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020052 vid_range_end = v->vid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080053 continue;
54 } else {
55 if ((vid_range_end - vid_range_start) > 0)
56 num_vlans += 2;
57 else
58 num_vlans += 1;
59 }
60initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020061 vid_range_start = v->vid;
62 vid_range_end = v->vid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080063 vid_range_flags = flags;
64 }
65
66 if (vid_range_start != 0) {
67 if ((vid_range_end - vid_range_start) > 0)
68 num_vlans += 2;
69 else
70 num_vlans += 1;
71 }
72
73 return num_vlans;
74}
75
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020076static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020077 u32 filter_mask)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020078{
79 if (!vg)
80 return 0;
81
82 if (filter_mask & RTEXT_FILTER_BRVLAN)
83 return vg->num_vlans;
84
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020085 return __get_num_vlan_infos(vg, filter_mask);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020086}
87
Roopa Prabhufed0a152015-02-25 23:55:40 -080088static size_t br_get_link_af_size_filtered(const struct net_device *dev,
89 u32 filter_mask)
Roopa Prabhub7853d72015-02-21 20:21:51 -080090{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020091 struct net_bridge_vlan_group *vg = NULL;
92 struct net_bridge_port *p;
93 struct net_bridge *br;
Roopa Prabhufed0a152015-02-25 23:55:40 -080094 int num_vlan_infos;
Roopa Prabhub7853d72015-02-21 20:21:51 -080095
Johannes Berg2f56f6b2015-03-03 16:02:16 +010096 rcu_read_lock();
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020097 if (br_port_exists(dev)) {
98 p = br_port_get_rcu(dev);
99 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200100 } else if (dev->priv_flags & IFF_EBRIDGE) {
101 br = netdev_priv(dev);
102 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200103 }
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200104 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100105 rcu_read_unlock();
Roopa Prabhub7853d72015-02-21 20:21:51 -0800106
Roopa Prabhub7853d72015-02-21 20:21:51 -0800107 /* Each VLAN is returned in bridge_vlan_info along with flags */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800108 return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhub7853d72015-02-21 20:21:51 -0800109}
110
stephen hemminger25c71c72012-11-13 07:53:05 +0000111static inline size_t br_port_info_size(void)
112{
113 return nla_total_size(1) /* IFLA_BRPORT_STATE */
114 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
115 + nla_total_size(4) /* IFLA_BRPORT_COST */
116 + nla_total_size(1) /* IFLA_BRPORT_MODE */
stephen hemmingera2e01a62012-11-13 07:53:07 +0000117 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
stephen hemminger1007dd12012-11-13 07:53:08 +0000118 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
stephen hemminger3da889b2013-03-11 13:52:17 +0000119 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400120 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400121 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200122 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200123 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
stephen hemminger25c71c72012-11-13 07:53:05 +0000124 + 0;
125}
126
Roopa Prabhufed0a152015-02-25 23:55:40 -0800127static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800128{
129 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000130 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
131 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
132 + nla_total_size(4) /* IFLA_MASTER */
133 + nla_total_size(4) /* IFLA_MTU */
134 + nla_total_size(4) /* IFLA_LINK */
135 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800136 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800137 + nla_total_size(br_get_link_af_size_filtered(dev,
138 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000139}
140
141static int br_port_fill_attrs(struct sk_buff *skb,
142 const struct net_bridge_port *p)
143{
144 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
145
146 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
147 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
148 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000149 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000150 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -0500151 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400152 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400153 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Kyeyoon Park95850112014-10-23 14:49:17 -0700154 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200155 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
156 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
157 !!(p->flags & BR_PROXYARP_WIFI)))
stephen hemminger25c71c72012-11-13 07:53:05 +0000158 return -EMSGSIZE;
159
160 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800161}
162
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800163static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
164 u16 vid_end, u16 flags)
165{
166 struct bridge_vlan_info vinfo;
167
168 if ((vid_end - vid_start) > 0) {
169 /* add range to skb */
170 vinfo.vid = vid_start;
171 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
172 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
173 sizeof(vinfo), &vinfo))
174 goto nla_put_failure;
175
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800176 vinfo.vid = vid_end;
177 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
178 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
179 sizeof(vinfo), &vinfo))
180 goto nla_put_failure;
181 } else {
182 vinfo.vid = vid_start;
183 vinfo.flags = flags;
184 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
185 sizeof(vinfo), &vinfo))
186 goto nla_put_failure;
187 }
188
189 return 0;
190
191nla_put_failure:
192 return -EMSGSIZE;
193}
194
195static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200196 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800197{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200198 struct net_bridge_vlan *v;
199 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200200 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800201 int err = 0;
202
203 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
204 * and mark vlan info with begin and end flags
205 * if vlaninfo represents a range
206 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200207 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200208 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800209 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200210 if (!br_vlan_should_use(v))
211 continue;
212 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800213 flags |= BRIDGE_VLAN_INFO_PVID;
214
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200215 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800216 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
217
218 if (vid_range_start == 0) {
219 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200220 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800221 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200222 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800223 continue;
224 } else {
225 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
226 vid_range_end,
227 vid_range_flags);
228 if (err)
229 return err;
230 }
231
232initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200233 vid_range_start = v->vid;
234 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800235 vid_range_flags = flags;
236 }
237
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800238 if (vid_range_start != 0) {
239 /* Call it once more to send any left over vlans */
240 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
241 vid_range_end,
242 vid_range_flags);
243 if (err)
244 return err;
245 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800246
247 return 0;
248}
249
250static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200251 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800252{
253 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200254 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200255 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800256
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200257 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200258 list_for_each_entry(v, &vg->vlan_list, vlist) {
259 if (!br_vlan_should_use(v))
260 continue;
261
262 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800263 vinfo.flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200264 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800265 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
266
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200267 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800268 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
269
270 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
271 sizeof(vinfo), &vinfo))
272 goto nla_put_failure;
273 }
274
275 return 0;
276
277nla_put_failure:
278 return -EMSGSIZE;
279}
280
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700281/*
282 * Create one netlink message for one interface
283 * Contains port and master info as well as carrier and bridge state.
284 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000285static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200286 struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000287 u32 pid, u32 seq, int event, unsigned int flags,
288 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700289{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200290 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800291 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700292 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700293 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700294
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000295 if (port)
296 br = port->br;
297 else
298 br = netdev_priv(dev);
299
stephen hemminger28a16c92010-05-10 09:31:09 +0000300 br_debug(br, "br_fill_info event %d port %s master %s\n",
301 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700302
Thomas Graf74685962006-11-20 16:20:22 -0800303 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
304 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800305 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700306
Thomas Graf74685962006-11-20 16:20:22 -0800307 hdr = nlmsg_data(nlh);
308 hdr->ifi_family = AF_BRIDGE;
309 hdr->__ifi_pad = 0;
310 hdr->ifi_type = dev->type;
311 hdr->ifi_index = dev->ifindex;
312 hdr->ifi_flags = dev_get_flags(dev);
313 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700314
David S. Miller2eb812e2012-04-01 20:49:54 -0400315 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
316 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
317 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
318 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
319 (dev->addr_len &&
320 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200321 (dev->ifindex != dev_get_iflink(dev) &&
322 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400323 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000324
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000325 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000326 struct nlattr *nest
327 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
328
329 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
330 goto nla_put_failure;
331 nla_nest_end(skb, nest);
332 }
333
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000334 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800335 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
336 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200337 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800338 struct nlattr *af;
339 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000340
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200341 if (port)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200342 vg = nbp_vlan_group(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200343 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200344 vg = br_vlan_group(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000345
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200346 if (!vg || !vg->num_vlans)
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000347 goto done;
348
349 af = nla_nest_start(skb, IFLA_AF_SPEC);
350 if (!af)
351 goto nla_put_failure;
352
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800353 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200354 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800355 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200356 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800357 if (err)
358 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000359 nla_nest_end(skb, af);
360 }
361
362done:
Johannes Berg053c0952015-01-16 22:09:00 +0100363 nlmsg_end(skb, nlh);
364 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700365
Thomas Graf74685962006-11-20 16:20:22 -0800366nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800367 nlmsg_cancel(skb, nlh);
368 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700369}
370
371/*
372 * Notify listeners of a change in port information
373 */
374void br_ifinfo_notify(int event, struct net_bridge_port *port)
375{
Vlad Yasevich407af322013-02-13 12:00:12 +0000376 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700377 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700378 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800379 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700380
Vlad Yasevich407af322013-02-13 12:00:12 +0000381 if (!port)
382 return;
383
384 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000385 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000386 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000387
Roopa Prabhufed0a152015-02-25 23:55:40 -0800388 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700389 if (skb == NULL)
390 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700391
Roopa Prabhufed0a152015-02-25 23:55:40 -0800392 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800393 if (err < 0) {
394 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
395 WARN_ON(err == -EMSGSIZE);
396 kfree_skb(skb);
397 goto errout;
398 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800399 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
400 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700401errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800402 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700403}
404
Vlad Yasevich407af322013-02-13 12:00:12 +0000405
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700406/*
407 * Dump information about all ports, in response to GETLINK
408 */
John Fastabende5a55a82012-10-24 08:12:57 +0000409int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200410 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700411{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800412 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700413
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800414 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
415 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300416 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000417
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200418 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300419 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700420}
421
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800422static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
423 int cmd, struct bridge_vlan_info *vinfo)
424{
425 int err = 0;
426
427 switch (cmd) {
428 case RTM_SETLINK:
429 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200430 /* if the MASTER flag is set this will act on the global
431 * per-VLAN entry as well
432 */
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800433 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
434 if (err)
435 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800436 } else {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200437 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800438 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
439 }
440 break;
441
442 case RTM_DELLINK:
443 if (p) {
444 nbp_vlan_delete(p, vinfo->vid);
445 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
446 br_vlan_delete(p->br, vinfo->vid);
447 } else {
448 br_vlan_delete(br, vinfo->vid);
449 }
450 break;
451 }
452
453 return err;
454}
Vlad Yasevich407af322013-02-13 12:00:12 +0000455
456static int br_afspec(struct net_bridge *br,
457 struct net_bridge_port *p,
458 struct nlattr *af_spec,
459 int cmd)
460{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800461 struct bridge_vlan_info *vinfo_start = NULL;
462 struct bridge_vlan_info *vinfo = NULL;
463 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000464 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800465 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000466
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800467 nla_for_each_nested(attr, af_spec, rem) {
468 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
469 continue;
470 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000471 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800472 vinfo = nla_data(attr);
Nikolay Aleksandrov462e1ea2015-07-02 05:48:17 -0700473 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
474 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800475 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
476 if (vinfo_start)
477 return -EINVAL;
478 vinfo_start = vinfo;
479 continue;
480 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000481
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800482 if (vinfo_start) {
483 struct bridge_vlan_info tmp_vinfo;
484 int v;
485
486 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
487 return -EINVAL;
488
489 if (vinfo->vid <= vinfo_start->vid)
490 return -EINVAL;
491
492 memcpy(&tmp_vinfo, vinfo_start,
493 sizeof(struct bridge_vlan_info));
494
495 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
496 tmp_vinfo.vid = v;
497 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000498 if (err)
499 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800500 }
501 vinfo_start = NULL;
502 } else {
503 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000504 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800505 if (err)
506 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000507 }
508
509 return err;
510}
511
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200512static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000513 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
514 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
515 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
516 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000517 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000518 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100519 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400520 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400521 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200522 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200523 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000524};
525
526/* Change the state of the port and notify spanning tree */
527static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700528{
stephen hemminger25c71c72012-11-13 07:53:05 +0000529 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000530 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700531
532 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700533 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700534 return -EBUSY;
535
stephen hemminger576eb622012-12-28 18:15:22 +0000536 /* if device is not up, change is not allowed
537 * if link is not present, only allowable state is disabled
538 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000539 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000540 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700541 return -ENETDOWN;
542
Florian Fainelli775dd692014-09-30 16:13:19 -0700543 br_set_state(p, state);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700544 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000545 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700546 return 0;
547}
548
stephen hemminger25c71c72012-11-13 07:53:05 +0000549/* Set/clear or port flags based on attribute */
550static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
551 int attrtype, unsigned long mask)
552{
553 if (tb[attrtype]) {
554 u8 flag = nla_get_u8(tb[attrtype]);
555 if (flag)
556 p->flags |= mask;
557 else
558 p->flags &= ~mask;
559 }
560}
561
562/* Process bridge protocol info on port */
563static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
564{
565 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400566 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000567
568 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000569 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500570 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000571 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400572 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400573 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Kyeyoon Park95850112014-10-23 14:49:17 -0700574 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200575 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
stephen hemminger25c71c72012-11-13 07:53:05 +0000576
577 if (tb[IFLA_BRPORT_COST]) {
578 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
579 if (err)
580 return err;
581 }
582
583 if (tb[IFLA_BRPORT_PRIORITY]) {
584 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
585 if (err)
586 return err;
587 }
588
589 if (tb[IFLA_BRPORT_STATE]) {
590 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
591 if (err)
592 return err;
593 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400594
595 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000596 return 0;
597}
598
599/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800600int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000601{
stephen hemminger25c71c72012-11-13 07:53:05 +0000602 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000603 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000604 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000605 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Scott Feldman41c498b2015-05-10 09:47:59 -0700606 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000607
Hong zhi guoc60ee672013-03-28 06:21:22 +0000608 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
609 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000610 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000611 return 0;
612
613 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000614 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800615 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000616 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000617 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000618 return -EINVAL;
619
Vlad Yasevich407af322013-02-13 12:00:12 +0000620 if (p && protinfo) {
621 if (protinfo->nla_type & NLA_F_NESTED) {
622 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200623 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000624 if (err)
625 return err;
626
627 spin_lock_bh(&p->br->lock);
628 err = br_setport(p, tb);
629 spin_unlock_bh(&p->br->lock);
630 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800631 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000632 if (nla_len(protinfo) < sizeof(u8))
633 return -EINVAL;
634
635 spin_lock_bh(&p->br->lock);
636 err = br_set_port_state(p, nla_get_u8(protinfo));
637 spin_unlock_bh(&p->br->lock);
638 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000639 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000640 goto out;
641 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000642
Vlad Yasevich407af322013-02-13 12:00:12 +0000643 if (afspec) {
644 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
645 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000646 }
647
648 if (err == 0)
649 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000650out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000651 return err;
652}
653
Vlad Yasevich407af322013-02-13 12:00:12 +0000654/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800655int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000656{
Vlad Yasevich407af322013-02-13 12:00:12 +0000657 struct nlattr *afspec;
658 struct net_bridge_port *p;
Scott Feldman85080252015-05-10 09:48:03 -0700659 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000660
Hong zhi guoc60ee672013-03-28 06:21:22 +0000661 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000662 if (!afspec)
663 return 0;
664
665 p = br_port_get_rtnl(dev);
666 /* We want to accept dev as bridge itself as well */
667 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
668 return -EINVAL;
669
670 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
671 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800672 if (err == 0)
673 /* Send RTM_NEWLINK because userspace
674 * expects RTM_NEWLINK for vlan dels
675 */
676 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000677
678 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
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900689 if (!data)
690 return 0;
691
692#ifdef CONFIG_BRIDGE_VLAN_FILTERING
693 if (data[IFLA_BR_VLAN_PROTOCOL]) {
694 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
695 case htons(ETH_P_8021Q):
696 case htons(ETH_P_8021AD):
697 break;
698 default:
699 return -EPROTONOSUPPORT;
700 }
701 }
702#endif
703
Thomas Graf32fe21c2007-03-22 11:59:03 -0700704 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700705}
706
Toshiaki Makita30313a32014-04-25 17:01:18 +0900707static int br_dev_newlink(struct net *src_net, struct net_device *dev,
708 struct nlattr *tb[], struct nlattr *data[])
709{
710 struct net_bridge *br = netdev_priv(dev);
711
712 if (tb[IFLA_ADDRESS]) {
713 spin_lock_bh(&br->lock);
714 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
715 spin_unlock_bh(&br->lock);
716 }
717
718 return register_netdevice(dev);
719}
720
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200721static int br_port_slave_changelink(struct net_device *brdev,
722 struct net_device *dev,
723 struct nlattr *tb[],
724 struct nlattr *data[])
725{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200726 struct net_bridge *br = netdev_priv(brdev);
727 int ret;
728
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200729 if (!data)
730 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200731
732 spin_lock_bh(&br->lock);
733 ret = br_setport(br_port_get_rtnl(dev), data);
734 spin_unlock_bh(&br->lock);
735
736 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200737}
738
Jiri Pirkoced82832014-09-05 15:51:29 +0200739static int br_port_fill_slave_info(struct sk_buff *skb,
740 const struct net_device *brdev,
741 const struct net_device *dev)
742{
743 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
744}
745
746static size_t br_port_get_slave_size(const struct net_device *brdev,
747 const struct net_device *dev)
748{
749 return br_port_info_size();
750}
751
Jiri Pirko13323512014-09-05 15:51:32 +0200752static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
753 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
754 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
755 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +0100756 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
757 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
758 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300759 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900760 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Jiri Pirko13323512014-09-05 15:51:32 +0200761};
762
763static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
764 struct nlattr *data[])
765{
766 struct net_bridge *br = netdev_priv(brdev);
767 int err;
768
769 if (!data)
770 return 0;
771
772 if (data[IFLA_BR_FORWARD_DELAY]) {
773 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
774 if (err)
775 return err;
776 }
777
778 if (data[IFLA_BR_HELLO_TIME]) {
779 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
780 if (err)
781 return err;
782 }
783
784 if (data[IFLA_BR_MAX_AGE]) {
785 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
786 if (err)
787 return err;
788 }
789
Jörg Thalheimaf615762015-03-18 10:06:58 +0100790 if (data[IFLA_BR_AGEING_TIME]) {
791 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
792
793 br->ageing_time = clock_t_to_jiffies(ageing_time);
794 }
795
796 if (data[IFLA_BR_STP_STATE]) {
797 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
798
799 br_stp_set_enabled(br, stp_enabled);
800 }
801
802 if (data[IFLA_BR_PRIORITY]) {
803 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
804
805 br_stp_set_bridge_priority(br, priority);
806 }
807
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300808 if (data[IFLA_BR_VLAN_FILTERING]) {
809 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
810
811 err = __br_vlan_filter_toggle(br, vlan_filter);
812 if (err)
813 return err;
814 }
815
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900816#ifdef CONFIG_BRIDGE_VLAN_FILTERING
817 if (data[IFLA_BR_VLAN_PROTOCOL]) {
818 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
819
820 err = __br_vlan_set_proto(br, vlan_proto);
821 if (err)
822 return err;
823 }
824#endif
825
Jiri Pirko13323512014-09-05 15:51:32 +0200826 return 0;
827}
828
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200829static size_t br_get_size(const struct net_device *brdev)
830{
831 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
832 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
833 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +0100834 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
835 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
836 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300837 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900838#ifdef CONFIG_BRIDGE_VLAN_FILTERING
839 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
840#endif
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200841 0;
842}
843
844static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
845{
846 struct net_bridge *br = netdev_priv(brdev);
847 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
848 u32 hello_time = jiffies_to_clock_t(br->hello_time);
849 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +0100850 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
851 u32 stp_enabled = br->stp_enabled;
852 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300853 u8 vlan_enabled = br_vlan_enabled(br);
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200854
855 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
856 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +0100857 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
858 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
859 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300860 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
861 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200862 return -EMSGSIZE;
863
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900864#ifdef CONFIG_BRIDGE_VLAN_FILTERING
865 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto))
866 return -EMSGSIZE;
867#endif
868
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200869 return 0;
870}
871
Roopa Prabhufed0a152015-02-25 23:55:40 -0800872static size_t br_get_link_af_size(const struct net_device *dev)
873{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200874 struct net_bridge_port *p;
875 struct net_bridge *br;
876 int num_vlans = 0;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800877
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200878 if (br_port_exists(dev)) {
879 p = br_port_get_rtnl(dev);
880 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200881 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200882 } else if (dev->priv_flags & IFF_EBRIDGE) {
883 br = netdev_priv(dev);
884 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200885 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200886 }
Roopa Prabhufed0a152015-02-25 23:55:40 -0800887
888 /* Each VLAN is returned in bridge_vlan_info along with flags */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200889 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhufed0a152015-02-25 23:55:40 -0800890}
891
Daniel Borkmann207895fd32015-01-29 12:15:03 +0100892static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000893 .family = AF_BRIDGE,
894 .get_link_af_size = br_get_link_af_size,
895};
896
stephen hemminger149ddd82012-06-26 05:48:45 +0000897struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +0200898 .kind = "bridge",
899 .priv_size = sizeof(struct net_bridge),
900 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -0700901 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +0200902 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +0200903 .validate = br_validate,
904 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +0200905 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +0200906 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200907 .get_size = br_get_size,
908 .fill_info = br_fill_info,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200909
910 .slave_maxtype = IFLA_BRPORT_MAX,
911 .slave_policy = br_port_policy,
912 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +0200913 .get_slave_size = br_port_get_slave_size,
914 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +0000915};
916
917int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700918{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000919 int err;
920
921 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -0800922 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000923
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000924 err = rtnl_link_register(&br_link_ops);
925 if (err)
926 goto out_af;
927
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000928 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000929
930out_af:
931 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000932 br_mdb_uninit();
933 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700934}
935
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +0200936void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +0000937{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +0000938 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000939 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +0000940 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +0000941}