blob: cad4050310ee227895b8c6e26e1d846b8ddc007d [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 */
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020037 list_for_each_entry_rcu(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{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020079 int num_vlans;
80
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020081 if (!vg)
82 return 0;
83
84 if (filter_mask & RTEXT_FILTER_BRVLAN)
85 return vg->num_vlans;
86
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020087 rcu_read_lock();
88 num_vlans = __get_num_vlan_infos(vg, filter_mask);
89 rcu_read_unlock();
90
91 return num_vlans;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020092}
93
Roopa Prabhufed0a152015-02-25 23:55:40 -080094static size_t br_get_link_af_size_filtered(const struct net_device *dev,
95 u32 filter_mask)
Roopa Prabhub7853d72015-02-21 20:21:51 -080096{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020097 struct net_bridge_vlan_group *vg = NULL;
98 struct net_bridge_port *p;
99 struct net_bridge *br;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800100 int num_vlan_infos;
Roopa Prabhub7853d72015-02-21 20:21:51 -0800101
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100102 rcu_read_lock();
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200103 if (br_port_exists(dev)) {
104 p = br_port_get_rcu(dev);
105 vg = nbp_vlan_group(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200106 } else if (dev->priv_flags & IFF_EBRIDGE) {
107 br = netdev_priv(dev);
108 vg = br_vlan_group(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200109 }
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200110 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100111 rcu_read_unlock();
Roopa Prabhub7853d72015-02-21 20:21:51 -0800112
Roopa Prabhub7853d72015-02-21 20:21:51 -0800113 /* Each VLAN is returned in bridge_vlan_info along with flags */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800114 return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhub7853d72015-02-21 20:21:51 -0800115}
116
stephen hemminger25c71c72012-11-13 07:53:05 +0000117static inline size_t br_port_info_size(void)
118{
119 return nla_total_size(1) /* IFLA_BRPORT_STATE */
120 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
121 + nla_total_size(4) /* IFLA_BRPORT_COST */
122 + nla_total_size(1) /* IFLA_BRPORT_MODE */
stephen hemmingera2e01a62012-11-13 07:53:07 +0000123 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
stephen hemminger1007dd12012-11-13 07:53:08 +0000124 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
stephen hemminger3da889b2013-03-11 13:52:17 +0000125 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400126 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400127 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200128 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200129 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200130 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
stephen hemminger25c71c72012-11-13 07:53:05 +0000131 + 0;
132}
133
Roopa Prabhufed0a152015-02-25 23:55:40 -0800134static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800135{
136 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000137 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
138 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
139 + nla_total_size(4) /* IFLA_MASTER */
140 + nla_total_size(4) /* IFLA_MTU */
141 + nla_total_size(4) /* IFLA_LINK */
142 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800143 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800144 + nla_total_size(br_get_link_af_size_filtered(dev,
145 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000146}
147
148static int br_port_fill_attrs(struct sk_buff *skb,
149 const struct net_bridge_port *p)
150{
151 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
152
153 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
154 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
155 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000156 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000157 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -0500158 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400159 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400160 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Kyeyoon Park95850112014-10-23 14:49:17 -0700161 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200162 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
163 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200164 !!(p->flags & BR_PROXYARP_WIFI)) ||
165 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
166 &p->designated_root))
stephen hemminger25c71c72012-11-13 07:53:05 +0000167 return -EMSGSIZE;
168
169 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800170}
171
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800172static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
173 u16 vid_end, u16 flags)
174{
175 struct bridge_vlan_info vinfo;
176
177 if ((vid_end - vid_start) > 0) {
178 /* add range to skb */
179 vinfo.vid = vid_start;
180 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
181 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
182 sizeof(vinfo), &vinfo))
183 goto nla_put_failure;
184
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800185 vinfo.vid = vid_end;
186 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
187 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
188 sizeof(vinfo), &vinfo))
189 goto nla_put_failure;
190 } else {
191 vinfo.vid = vid_start;
192 vinfo.flags = flags;
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
204static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200205 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800206{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200207 struct net_bridge_vlan *v;
208 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200209 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800210 int err = 0;
211
212 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
213 * and mark vlan info with begin and end flags
214 * if vlaninfo represents a range
215 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200216 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200217 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800218 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200219 if (!br_vlan_should_use(v))
220 continue;
221 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800222 flags |= BRIDGE_VLAN_INFO_PVID;
223
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200224 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800225 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
226
227 if (vid_range_start == 0) {
228 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200229 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800230 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200231 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800232 continue;
233 } else {
234 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
235 vid_range_end,
236 vid_range_flags);
237 if (err)
238 return err;
239 }
240
241initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200242 vid_range_start = v->vid;
243 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800244 vid_range_flags = flags;
245 }
246
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800247 if (vid_range_start != 0) {
248 /* Call it once more to send any left over vlans */
249 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
250 vid_range_end,
251 vid_range_flags);
252 if (err)
253 return err;
254 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800255
256 return 0;
257}
258
259static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200260 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800261{
262 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200263 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200264 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800265
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200266 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200267 list_for_each_entry(v, &vg->vlan_list, vlist) {
268 if (!br_vlan_should_use(v))
269 continue;
270
271 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800272 vinfo.flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200273 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800274 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
275
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200276 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800277 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
278
279 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
280 sizeof(vinfo), &vinfo))
281 goto nla_put_failure;
282 }
283
284 return 0;
285
286nla_put_failure:
287 return -EMSGSIZE;
288}
289
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700290/*
291 * Create one netlink message for one interface
292 * Contains port and master info as well as carrier and bridge state.
293 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000294static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200295 struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000296 u32 pid, u32 seq, int event, unsigned int flags,
297 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700298{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200299 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800300 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700301 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700302 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700303
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000304 if (port)
305 br = port->br;
306 else
307 br = netdev_priv(dev);
308
stephen hemminger28a16c92010-05-10 09:31:09 +0000309 br_debug(br, "br_fill_info event %d port %s master %s\n",
310 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700311
Thomas Graf74685962006-11-20 16:20:22 -0800312 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
313 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800314 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700315
Thomas Graf74685962006-11-20 16:20:22 -0800316 hdr = nlmsg_data(nlh);
317 hdr->ifi_family = AF_BRIDGE;
318 hdr->__ifi_pad = 0;
319 hdr->ifi_type = dev->type;
320 hdr->ifi_index = dev->ifindex;
321 hdr->ifi_flags = dev_get_flags(dev);
322 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700323
David S. Miller2eb812e2012-04-01 20:49:54 -0400324 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
325 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
326 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
327 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
328 (dev->addr_len &&
329 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200330 (dev->ifindex != dev_get_iflink(dev) &&
331 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400332 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000333
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000334 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000335 struct nlattr *nest
336 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
337
338 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
339 goto nla_put_failure;
340 nla_nest_end(skb, nest);
341 }
342
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000343 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800344 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
345 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200346 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800347 struct nlattr *af;
348 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000349
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200350 if (port)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200351 vg = nbp_vlan_group(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200352 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200353 vg = br_vlan_group(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000354
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200355 if (!vg || !vg->num_vlans)
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000356 goto done;
357
358 af = nla_nest_start(skb, IFLA_AF_SPEC);
359 if (!af)
360 goto nla_put_failure;
361
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800362 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200363 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800364 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200365 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800366 if (err)
367 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000368 nla_nest_end(skb, af);
369 }
370
371done:
Johannes Berg053c0952015-01-16 22:09:00 +0100372 nlmsg_end(skb, nlh);
373 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700374
Thomas Graf74685962006-11-20 16:20:22 -0800375nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800376 nlmsg_cancel(skb, nlh);
377 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700378}
379
380/*
381 * Notify listeners of a change in port information
382 */
383void br_ifinfo_notify(int event, struct net_bridge_port *port)
384{
Vlad Yasevich407af322013-02-13 12:00:12 +0000385 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700386 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700387 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800388 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700389
Vlad Yasevich407af322013-02-13 12:00:12 +0000390 if (!port)
391 return;
392
393 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000394 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000395 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000396
Roopa Prabhufed0a152015-02-25 23:55:40 -0800397 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700398 if (skb == NULL)
399 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700400
Roopa Prabhufed0a152015-02-25 23:55:40 -0800401 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800402 if (err < 0) {
403 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
404 WARN_ON(err == -EMSGSIZE);
405 kfree_skb(skb);
406 goto errout;
407 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800408 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
409 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700410errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800411 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700412}
413
Vlad Yasevich407af322013-02-13 12:00:12 +0000414
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700415/*
416 * Dump information about all ports, in response to GETLINK
417 */
John Fastabende5a55a82012-10-24 08:12:57 +0000418int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200419 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700420{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800421 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700422
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800423 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
424 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300425 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000426
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200427 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300428 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700429}
430
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800431static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
432 int cmd, struct bridge_vlan_info *vinfo)
433{
434 int err = 0;
435
436 switch (cmd) {
437 case RTM_SETLINK:
438 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200439 /* if the MASTER flag is set this will act on the global
440 * per-VLAN entry as well
441 */
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800442 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
443 if (err)
444 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800445 } else {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200446 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800447 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
448 }
449 break;
450
451 case RTM_DELLINK:
452 if (p) {
453 nbp_vlan_delete(p, vinfo->vid);
454 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
455 br_vlan_delete(p->br, vinfo->vid);
456 } else {
457 br_vlan_delete(br, vinfo->vid);
458 }
459 break;
460 }
461
462 return err;
463}
Vlad Yasevich407af322013-02-13 12:00:12 +0000464
465static int br_afspec(struct net_bridge *br,
466 struct net_bridge_port *p,
467 struct nlattr *af_spec,
468 int cmd)
469{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800470 struct bridge_vlan_info *vinfo_start = NULL;
471 struct bridge_vlan_info *vinfo = NULL;
472 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000473 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800474 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000475
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800476 nla_for_each_nested(attr, af_spec, rem) {
477 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
478 continue;
479 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000480 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800481 vinfo = nla_data(attr);
Nikolay Aleksandrov462e1ea2015-07-02 05:48:17 -0700482 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
483 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800484 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
485 if (vinfo_start)
486 return -EINVAL;
487 vinfo_start = vinfo;
488 continue;
489 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000490
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800491 if (vinfo_start) {
492 struct bridge_vlan_info tmp_vinfo;
493 int v;
494
495 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
496 return -EINVAL;
497
498 if (vinfo->vid <= vinfo_start->vid)
499 return -EINVAL;
500
501 memcpy(&tmp_vinfo, vinfo_start,
502 sizeof(struct bridge_vlan_info));
503
504 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
505 tmp_vinfo.vid = v;
506 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000507 if (err)
508 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800509 }
510 vinfo_start = NULL;
511 } else {
512 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000513 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800514 if (err)
515 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000516 }
517
518 return err;
519}
520
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200521static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000522 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
523 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
524 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
525 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000526 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000527 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100528 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400529 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400530 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200531 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200532 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000533};
534
535/* Change the state of the port and notify spanning tree */
536static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700537{
stephen hemminger25c71c72012-11-13 07:53:05 +0000538 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000539 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700540
541 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700542 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700543 return -EBUSY;
544
stephen hemminger576eb622012-12-28 18:15:22 +0000545 /* if device is not up, change is not allowed
546 * if link is not present, only allowable state is disabled
547 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000548 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000549 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700550 return -ENETDOWN;
551
Florian Fainelli775dd692014-09-30 16:13:19 -0700552 br_set_state(p, state);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700553 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000554 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700555 return 0;
556}
557
stephen hemminger25c71c72012-11-13 07:53:05 +0000558/* Set/clear or port flags based on attribute */
559static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
560 int attrtype, unsigned long mask)
561{
562 if (tb[attrtype]) {
563 u8 flag = nla_get_u8(tb[attrtype]);
564 if (flag)
565 p->flags |= mask;
566 else
567 p->flags &= ~mask;
568 }
569}
570
571/* Process bridge protocol info on port */
572static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
573{
574 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400575 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000576
577 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000578 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500579 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000580 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400581 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400582 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Kyeyoon Park95850112014-10-23 14:49:17 -0700583 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200584 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
stephen hemminger25c71c72012-11-13 07:53:05 +0000585
586 if (tb[IFLA_BRPORT_COST]) {
587 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
588 if (err)
589 return err;
590 }
591
592 if (tb[IFLA_BRPORT_PRIORITY]) {
593 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
594 if (err)
595 return err;
596 }
597
598 if (tb[IFLA_BRPORT_STATE]) {
599 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
600 if (err)
601 return err;
602 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400603
604 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000605 return 0;
606}
607
608/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800609int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000610{
stephen hemminger25c71c72012-11-13 07:53:05 +0000611 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000612 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000613 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000614 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Scott Feldman41c498b2015-05-10 09:47:59 -0700615 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000616
Hong zhi guoc60ee672013-03-28 06:21:22 +0000617 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
618 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000619 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000620 return 0;
621
622 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000623 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800624 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000625 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000626 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000627 return -EINVAL;
628
Vlad Yasevich407af322013-02-13 12:00:12 +0000629 if (p && protinfo) {
630 if (protinfo->nla_type & NLA_F_NESTED) {
631 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200632 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000633 if (err)
634 return err;
635
636 spin_lock_bh(&p->br->lock);
637 err = br_setport(p, tb);
638 spin_unlock_bh(&p->br->lock);
639 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800640 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000641 if (nla_len(protinfo) < sizeof(u8))
642 return -EINVAL;
643
644 spin_lock_bh(&p->br->lock);
645 err = br_set_port_state(p, nla_get_u8(protinfo));
646 spin_unlock_bh(&p->br->lock);
647 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000648 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000649 goto out;
650 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000651
Vlad Yasevich407af322013-02-13 12:00:12 +0000652 if (afspec) {
653 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
654 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000655 }
656
657 if (err == 0)
658 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000659out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000660 return err;
661}
662
Vlad Yasevich407af322013-02-13 12:00:12 +0000663/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800664int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000665{
Vlad Yasevich407af322013-02-13 12:00:12 +0000666 struct nlattr *afspec;
667 struct net_bridge_port *p;
Scott Feldman85080252015-05-10 09:48:03 -0700668 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000669
Hong zhi guoc60ee672013-03-28 06:21:22 +0000670 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000671 if (!afspec)
672 return 0;
673
674 p = br_port_get_rtnl(dev);
675 /* We want to accept dev as bridge itself as well */
676 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
677 return -EINVAL;
678
679 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
680 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800681 if (err == 0)
682 /* Send RTM_NEWLINK because userspace
683 * expects RTM_NEWLINK for vlan dels
684 */
685 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000686
687 return err;
688}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000689static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700690{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000691 if (tb[IFLA_ADDRESS]) {
692 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
693 return -EINVAL;
694 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
695 return -EADDRNOTAVAIL;
696 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700697
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900698 if (!data)
699 return 0;
700
701#ifdef CONFIG_BRIDGE_VLAN_FILTERING
702 if (data[IFLA_BR_VLAN_PROTOCOL]) {
703 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
704 case htons(ETH_P_8021Q):
705 case htons(ETH_P_8021AD):
706 break;
707 default:
708 return -EPROTONOSUPPORT;
709 }
710 }
711#endif
712
Thomas Graf32fe21c2007-03-22 11:59:03 -0700713 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700714}
715
Toshiaki Makita30313a32014-04-25 17:01:18 +0900716static int br_dev_newlink(struct net *src_net, struct net_device *dev,
717 struct nlattr *tb[], struct nlattr *data[])
718{
719 struct net_bridge *br = netdev_priv(dev);
720
721 if (tb[IFLA_ADDRESS]) {
722 spin_lock_bh(&br->lock);
723 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
724 spin_unlock_bh(&br->lock);
725 }
726
727 return register_netdevice(dev);
728}
729
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200730static int br_port_slave_changelink(struct net_device *brdev,
731 struct net_device *dev,
732 struct nlattr *tb[],
733 struct nlattr *data[])
734{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200735 struct net_bridge *br = netdev_priv(brdev);
736 int ret;
737
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200738 if (!data)
739 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200740
741 spin_lock_bh(&br->lock);
742 ret = br_setport(br_port_get_rtnl(dev), data);
743 spin_unlock_bh(&br->lock);
744
745 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200746}
747
Jiri Pirkoced82832014-09-05 15:51:29 +0200748static int br_port_fill_slave_info(struct sk_buff *skb,
749 const struct net_device *brdev,
750 const struct net_device *dev)
751{
752 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
753}
754
755static size_t br_port_get_slave_size(const struct net_device *brdev,
756 const struct net_device *dev)
757{
758 return br_port_info_size();
759}
760
Jiri Pirko13323512014-09-05 15:51:32 +0200761static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
762 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
763 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
764 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +0100765 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
766 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
767 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300768 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900769 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200770 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200771 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
772 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200773 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200774 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200775 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200776 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200777 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200778 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200779 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200780 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200781 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
782 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
783 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
784 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
785 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
786 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200787 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
788 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
789 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200790 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
Jiri Pirko13323512014-09-05 15:51:32 +0200791};
792
793static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
794 struct nlattr *data[])
795{
796 struct net_bridge *br = netdev_priv(brdev);
797 int err;
798
799 if (!data)
800 return 0;
801
802 if (data[IFLA_BR_FORWARD_DELAY]) {
803 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
804 if (err)
805 return err;
806 }
807
808 if (data[IFLA_BR_HELLO_TIME]) {
809 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
810 if (err)
811 return err;
812 }
813
814 if (data[IFLA_BR_MAX_AGE]) {
815 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
816 if (err)
817 return err;
818 }
819
Jörg Thalheimaf615762015-03-18 10:06:58 +0100820 if (data[IFLA_BR_AGEING_TIME]) {
821 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
822
823 br->ageing_time = clock_t_to_jiffies(ageing_time);
824 }
825
826 if (data[IFLA_BR_STP_STATE]) {
827 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
828
829 br_stp_set_enabled(br, stp_enabled);
830 }
831
832 if (data[IFLA_BR_PRIORITY]) {
833 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
834
835 br_stp_set_bridge_priority(br, priority);
836 }
837
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300838 if (data[IFLA_BR_VLAN_FILTERING]) {
839 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
840
841 err = __br_vlan_filter_toggle(br, vlan_filter);
842 if (err)
843 return err;
844 }
845
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900846#ifdef CONFIG_BRIDGE_VLAN_FILTERING
847 if (data[IFLA_BR_VLAN_PROTOCOL]) {
848 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
849
850 err = __br_vlan_set_proto(br, vlan_proto);
851 if (err)
852 return err;
853 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200854
855 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
856 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
857
858 err = __br_vlan_set_default_pvid(br, defpvid);
859 if (err)
860 return err;
861 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900862#endif
863
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200864 if (data[IFLA_BR_GROUP_FWD_MASK]) {
865 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
866
867 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
868 return -EINVAL;
869 br->group_fwd_mask = fwd_mask;
870 }
871
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200872 if (data[IFLA_BR_GROUP_ADDR]) {
873 u8 new_addr[ETH_ALEN];
874
875 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
876 return -EINVAL;
877 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
878 if (!is_link_local_ether_addr(new_addr))
879 return -EINVAL;
880 if (new_addr[5] == 1 || /* 802.3x Pause address */
881 new_addr[5] == 2 || /* 802.3ad Slow protocols */
882 new_addr[5] == 3) /* 802.1X PAE address */
883 return -EINVAL;
884 spin_lock_bh(&br->lock);
885 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
886 spin_unlock_bh(&br->lock);
887 br->group_addr_set = true;
888 br_recalculate_fwd_mask(br);
889 }
890
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +0200891 if (data[IFLA_BR_FDB_FLUSH])
892 br_fdb_flush(br);
893
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200894#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
895 if (data[IFLA_BR_MCAST_ROUTER]) {
896 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
897
898 err = br_multicast_set_router(br, multicast_router);
899 if (err)
900 return err;
901 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200902
903 if (data[IFLA_BR_MCAST_SNOOPING]) {
904 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
905
906 err = br_multicast_toggle(br, mcast_snooping);
907 if (err)
908 return err;
909 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200910
911 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
912 u8 val;
913
914 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
915 br->multicast_query_use_ifaddr = !!val;
916 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200917
918 if (data[IFLA_BR_MCAST_QUERIER]) {
919 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
920
921 err = br_multicast_set_querier(br, mcast_querier);
922 if (err)
923 return err;
924 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200925
926 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
927 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
928
929 br->hash_elasticity = val;
930 }
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200931
932 if (data[IFLA_BR_MCAST_HASH_MAX]) {
933 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
934
935 err = br_multicast_set_hash_max(br, hash_max);
936 if (err)
937 return err;
938 }
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200939
940 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
941 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
942
943 br->multicast_last_member_count = val;
944 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200945
946 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
947 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
948
949 br->multicast_startup_query_count = val;
950 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200951
952 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
953 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
954
955 br->multicast_last_member_interval = clock_t_to_jiffies(val);
956 }
957
958 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
959 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
960
961 br->multicast_membership_interval = clock_t_to_jiffies(val);
962 }
963
964 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
965 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
966
967 br->multicast_querier_interval = clock_t_to_jiffies(val);
968 }
969
970 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
971 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
972
973 br->multicast_query_interval = clock_t_to_jiffies(val);
974 }
975
976 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
977 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
978
979 br->multicast_query_response_interval = clock_t_to_jiffies(val);
980 }
981
982 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
983 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
984
985 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
986 }
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200987#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200988#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
989 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
990 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
991
992 br->nf_call_iptables = val ? true : false;
993 }
994
995 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
996 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
997
998 br->nf_call_ip6tables = val ? true : false;
999 }
1000
1001 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1002 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1003
1004 br->nf_call_arptables = val ? true : false;
1005 }
1006#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001007
Jiri Pirko13323512014-09-05 15:51:32 +02001008 return 0;
1009}
1010
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001011static size_t br_get_size(const struct net_device *brdev)
1012{
1013 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1014 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1015 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001016 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1017 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1018 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001019 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001020#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1021 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001022 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001023#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001024 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001025 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001026 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001027 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001028 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001029 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1030 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001031 nla_total_size(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1032 nla_total_size(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1033 nla_total_size(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1034 nla_total_size(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001035 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001036#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1037 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001038 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001039 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001040 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001041 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1042 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1043 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1044 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001045 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1046 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1047 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1048 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1049 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1050 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001051#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001052#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1053 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1054 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1055 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1056#endif
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001057 0;
1058}
1059
1060static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1061{
1062 struct net_bridge *br = netdev_priv(brdev);
1063 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1064 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1065 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001066 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1067 u32 stp_enabled = br->stp_enabled;
1068 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001069 u8 vlan_enabled = br_vlan_enabled(br);
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001070 u64 clockval;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001071
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001072 clockval = br_timer_value(&br->hello_timer);
1073 if (nla_put_u64(skb, IFLA_BR_HELLO_TIMER, clockval))
1074 return -EMSGSIZE;
1075 clockval = br_timer_value(&br->tcn_timer);
1076 if (nla_put_u64(skb, IFLA_BR_TCN_TIMER, clockval))
1077 return -EMSGSIZE;
1078 clockval = br_timer_value(&br->topology_change_timer);
1079 if (nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval))
1080 return -EMSGSIZE;
1081 clockval = br_timer_value(&br->gc_timer);
1082 if (nla_put_u64(skb, IFLA_BR_GC_TIMER, clockval))
1083 return -EMSGSIZE;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001084
1085 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1086 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001087 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1088 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1089 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001090 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001091 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001092 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1093 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1094 &br->bridge_id) ||
1095 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1096 &br->designated_root) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001097 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001098 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1099 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1100 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001101 br->topology_change_detected) ||
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001102 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001103 return -EMSGSIZE;
1104
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001105#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001106 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1107 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001108 return -EMSGSIZE;
1109#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001110#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001111 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001112 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1113 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001114 br->multicast_query_use_ifaddr) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001115 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1116 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001117 br->hash_elasticity) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001118 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1119 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001120 br->multicast_last_member_count) ||
1121 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1122 br->multicast_startup_query_count))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001123 return -EMSGSIZE;
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001124
1125 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1126 if (nla_put_u64(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval))
1127 return -EMSGSIZE;
1128 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1129 if (nla_put_u64(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval))
1130 return -EMSGSIZE;
1131 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1132 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval))
1133 return -EMSGSIZE;
1134 clockval = jiffies_to_clock_t(br->multicast_query_interval);
1135 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval))
1136 return -EMSGSIZE;
1137 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1138 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval))
1139 return -EMSGSIZE;
1140 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1141 if (nla_put_u64(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval))
1142 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001143#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001144#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1145 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1146 br->nf_call_iptables ? 1 : 0) ||
1147 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1148 br->nf_call_ip6tables ? 1 : 0) ||
1149 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1150 br->nf_call_arptables ? 1 : 0))
1151 return -EMSGSIZE;
1152#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001153
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001154 return 0;
1155}
1156
Roopa Prabhufed0a152015-02-25 23:55:40 -08001157static size_t br_get_link_af_size(const struct net_device *dev)
1158{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001159 struct net_bridge_port *p;
1160 struct net_bridge *br;
1161 int num_vlans = 0;
Roopa Prabhufed0a152015-02-25 23:55:40 -08001162
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001163 if (br_port_exists(dev)) {
1164 p = br_port_get_rtnl(dev);
1165 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +02001166 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001167 } else if (dev->priv_flags & IFF_EBRIDGE) {
1168 br = netdev_priv(dev);
1169 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +02001170 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001171 }
Roopa Prabhufed0a152015-02-25 23:55:40 -08001172
1173 /* Each VLAN is returned in bridge_vlan_info along with flags */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001174 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhufed0a152015-02-25 23:55:40 -08001175}
1176
Daniel Borkmann207895fd32015-01-29 12:15:03 +01001177static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001178 .family = AF_BRIDGE,
1179 .get_link_af_size = br_get_link_af_size,
1180};
1181
stephen hemminger149ddd82012-06-26 05:48:45 +00001182struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001183 .kind = "bridge",
1184 .priv_size = sizeof(struct net_bridge),
1185 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001186 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001187 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001188 .validate = br_validate,
1189 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001190 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001191 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001192 .get_size = br_get_size,
1193 .fill_info = br_fill_info,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001194
1195 .slave_maxtype = IFLA_BRPORT_MAX,
1196 .slave_policy = br_port_policy,
1197 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001198 .get_slave_size = br_port_get_slave_size,
1199 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001200};
1201
1202int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001203{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001204 int err;
1205
1206 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001207 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001208
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001209 err = rtnl_link_register(&br_link_ops);
1210 if (err)
1211 goto out_af;
1212
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001213 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001214
1215out_af:
1216 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001217 br_mdb_uninit();
1218 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001219}
1220
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001221void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001222{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001223 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001224 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001225 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001226}