blob: c3e0b73d660d177dd3de2a3493d99ae29bee9b8f [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 */
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200131 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
stephen hemminger25c71c72012-11-13 07:53:05 +0000132 + 0;
133}
134
Roopa Prabhufed0a152015-02-25 23:55:40 -0800135static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800136{
137 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000138 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
139 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
140 + nla_total_size(4) /* IFLA_MASTER */
141 + nla_total_size(4) /* IFLA_MTU */
142 + nla_total_size(4) /* IFLA_LINK */
143 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800144 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800145 + nla_total_size(br_get_link_af_size_filtered(dev,
146 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000147}
148
149static int br_port_fill_attrs(struct sk_buff *skb,
150 const struct net_bridge_port *p)
151{
152 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
153
154 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
155 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
156 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000157 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000158 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -0500159 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400160 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400161 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Kyeyoon Park95850112014-10-23 14:49:17 -0700162 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200163 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
164 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200165 !!(p->flags & BR_PROXYARP_WIFI)) ||
166 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200167 &p->designated_root) ||
168 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
169 &p->designated_bridge))
stephen hemminger25c71c72012-11-13 07:53:05 +0000170 return -EMSGSIZE;
171
172 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800173}
174
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800175static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
176 u16 vid_end, u16 flags)
177{
178 struct bridge_vlan_info vinfo;
179
180 if ((vid_end - vid_start) > 0) {
181 /* add range to skb */
182 vinfo.vid = vid_start;
183 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
184 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
185 sizeof(vinfo), &vinfo))
186 goto nla_put_failure;
187
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800188 vinfo.vid = vid_end;
189 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
190 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
191 sizeof(vinfo), &vinfo))
192 goto nla_put_failure;
193 } else {
194 vinfo.vid = vid_start;
195 vinfo.flags = flags;
196 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
197 sizeof(vinfo), &vinfo))
198 goto nla_put_failure;
199 }
200
201 return 0;
202
203nla_put_failure:
204 return -EMSGSIZE;
205}
206
207static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200208 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800209{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200210 struct net_bridge_vlan *v;
211 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200212 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800213 int err = 0;
214
215 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
216 * and mark vlan info with begin and end flags
217 * if vlaninfo represents a range
218 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200219 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200220 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800221 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200222 if (!br_vlan_should_use(v))
223 continue;
224 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800225 flags |= BRIDGE_VLAN_INFO_PVID;
226
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200227 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800228 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
229
230 if (vid_range_start == 0) {
231 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200232 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800233 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200234 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800235 continue;
236 } else {
237 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
238 vid_range_end,
239 vid_range_flags);
240 if (err)
241 return err;
242 }
243
244initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200245 vid_range_start = v->vid;
246 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800247 vid_range_flags = flags;
248 }
249
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800250 if (vid_range_start != 0) {
251 /* Call it once more to send any left over vlans */
252 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
253 vid_range_end,
254 vid_range_flags);
255 if (err)
256 return err;
257 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800258
259 return 0;
260}
261
262static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200263 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800264{
265 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200266 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200267 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800268
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200269 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200270 list_for_each_entry(v, &vg->vlan_list, vlist) {
271 if (!br_vlan_should_use(v))
272 continue;
273
274 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800275 vinfo.flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200276 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800277 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
278
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200279 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800280 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
281
282 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
283 sizeof(vinfo), &vinfo))
284 goto nla_put_failure;
285 }
286
287 return 0;
288
289nla_put_failure:
290 return -EMSGSIZE;
291}
292
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700293/*
294 * Create one netlink message for one interface
295 * Contains port and master info as well as carrier and bridge state.
296 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000297static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200298 struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000299 u32 pid, u32 seq, int event, unsigned int flags,
300 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700301{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200302 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800303 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700304 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700305 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700306
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000307 if (port)
308 br = port->br;
309 else
310 br = netdev_priv(dev);
311
stephen hemminger28a16c92010-05-10 09:31:09 +0000312 br_debug(br, "br_fill_info event %d port %s master %s\n",
313 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700314
Thomas Graf74685962006-11-20 16:20:22 -0800315 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
316 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800317 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700318
Thomas Graf74685962006-11-20 16:20:22 -0800319 hdr = nlmsg_data(nlh);
320 hdr->ifi_family = AF_BRIDGE;
321 hdr->__ifi_pad = 0;
322 hdr->ifi_type = dev->type;
323 hdr->ifi_index = dev->ifindex;
324 hdr->ifi_flags = dev_get_flags(dev);
325 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700326
David S. Miller2eb812e2012-04-01 20:49:54 -0400327 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
328 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
329 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
330 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
331 (dev->addr_len &&
332 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200333 (dev->ifindex != dev_get_iflink(dev) &&
334 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400335 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000336
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000337 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000338 struct nlattr *nest
339 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
340
341 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
342 goto nla_put_failure;
343 nla_nest_end(skb, nest);
344 }
345
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000346 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800347 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
348 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200349 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800350 struct nlattr *af;
351 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000352
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200353 if (port)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200354 vg = nbp_vlan_group(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200355 else
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200356 vg = br_vlan_group(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000357
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200358 if (!vg || !vg->num_vlans)
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000359 goto done;
360
361 af = nla_nest_start(skb, IFLA_AF_SPEC);
362 if (!af)
363 goto nla_put_failure;
364
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800365 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200366 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800367 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200368 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800369 if (err)
370 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000371 nla_nest_end(skb, af);
372 }
373
374done:
Johannes Berg053c0952015-01-16 22:09:00 +0100375 nlmsg_end(skb, nlh);
376 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700377
Thomas Graf74685962006-11-20 16:20:22 -0800378nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800379 nlmsg_cancel(skb, nlh);
380 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700381}
382
383/*
384 * Notify listeners of a change in port information
385 */
386void br_ifinfo_notify(int event, struct net_bridge_port *port)
387{
Vlad Yasevich407af322013-02-13 12:00:12 +0000388 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700389 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700390 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800391 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700392
Vlad Yasevich407af322013-02-13 12:00:12 +0000393 if (!port)
394 return;
395
396 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000397 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000398 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000399
Roopa Prabhufed0a152015-02-25 23:55:40 -0800400 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700401 if (skb == NULL)
402 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700403
Roopa Prabhufed0a152015-02-25 23:55:40 -0800404 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800405 if (err < 0) {
406 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
407 WARN_ON(err == -EMSGSIZE);
408 kfree_skb(skb);
409 goto errout;
410 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800411 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
412 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700413errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800414 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700415}
416
Vlad Yasevich407af322013-02-13 12:00:12 +0000417
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700418/*
419 * Dump information about all ports, in response to GETLINK
420 */
John Fastabende5a55a82012-10-24 08:12:57 +0000421int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200422 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700423{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800424 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700425
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800426 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
427 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300428 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000429
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200430 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300431 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700432}
433
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800434static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
435 int cmd, struct bridge_vlan_info *vinfo)
436{
437 int err = 0;
438
439 switch (cmd) {
440 case RTM_SETLINK:
441 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200442 /* if the MASTER flag is set this will act on the global
443 * per-VLAN entry as well
444 */
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800445 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
446 if (err)
447 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800448 } else {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200449 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800450 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
451 }
452 break;
453
454 case RTM_DELLINK:
455 if (p) {
456 nbp_vlan_delete(p, vinfo->vid);
457 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
458 br_vlan_delete(p->br, vinfo->vid);
459 } else {
460 br_vlan_delete(br, vinfo->vid);
461 }
462 break;
463 }
464
465 return err;
466}
Vlad Yasevich407af322013-02-13 12:00:12 +0000467
468static int br_afspec(struct net_bridge *br,
469 struct net_bridge_port *p,
470 struct nlattr *af_spec,
471 int cmd)
472{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800473 struct bridge_vlan_info *vinfo_start = NULL;
474 struct bridge_vlan_info *vinfo = NULL;
475 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000476 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800477 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000478
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800479 nla_for_each_nested(attr, af_spec, rem) {
480 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
481 continue;
482 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000483 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800484 vinfo = nla_data(attr);
Nikolay Aleksandrov462e1ea2015-07-02 05:48:17 -0700485 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
486 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800487 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
488 if (vinfo_start)
489 return -EINVAL;
490 vinfo_start = vinfo;
491 continue;
492 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000493
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800494 if (vinfo_start) {
495 struct bridge_vlan_info tmp_vinfo;
496 int v;
497
498 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
499 return -EINVAL;
500
501 if (vinfo->vid <= vinfo_start->vid)
502 return -EINVAL;
503
504 memcpy(&tmp_vinfo, vinfo_start,
505 sizeof(struct bridge_vlan_info));
506
507 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
508 tmp_vinfo.vid = v;
509 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000510 if (err)
511 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800512 }
513 vinfo_start = NULL;
514 } else {
515 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000516 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800517 if (err)
518 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000519 }
520
521 return err;
522}
523
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200524static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000525 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
526 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
527 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
528 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000529 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000530 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100531 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400532 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400533 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200534 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200535 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000536};
537
538/* Change the state of the port and notify spanning tree */
539static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700540{
stephen hemminger25c71c72012-11-13 07:53:05 +0000541 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000542 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700543
544 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700545 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700546 return -EBUSY;
547
stephen hemminger576eb622012-12-28 18:15:22 +0000548 /* if device is not up, change is not allowed
549 * if link is not present, only allowable state is disabled
550 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000551 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000552 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700553 return -ENETDOWN;
554
Florian Fainelli775dd692014-09-30 16:13:19 -0700555 br_set_state(p, state);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700556 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000557 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700558 return 0;
559}
560
stephen hemminger25c71c72012-11-13 07:53:05 +0000561/* Set/clear or port flags based on attribute */
562static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
563 int attrtype, unsigned long mask)
564{
565 if (tb[attrtype]) {
566 u8 flag = nla_get_u8(tb[attrtype]);
567 if (flag)
568 p->flags |= mask;
569 else
570 p->flags &= ~mask;
571 }
572}
573
574/* Process bridge protocol info on port */
575static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
576{
577 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400578 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000579
580 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000581 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500582 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000583 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400584 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400585 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Kyeyoon Park95850112014-10-23 14:49:17 -0700586 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200587 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
stephen hemminger25c71c72012-11-13 07:53:05 +0000588
589 if (tb[IFLA_BRPORT_COST]) {
590 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
591 if (err)
592 return err;
593 }
594
595 if (tb[IFLA_BRPORT_PRIORITY]) {
596 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
597 if (err)
598 return err;
599 }
600
601 if (tb[IFLA_BRPORT_STATE]) {
602 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
603 if (err)
604 return err;
605 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400606
607 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000608 return 0;
609}
610
611/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800612int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000613{
stephen hemminger25c71c72012-11-13 07:53:05 +0000614 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000615 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000616 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000617 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Scott Feldman41c498b2015-05-10 09:47:59 -0700618 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000619
Hong zhi guoc60ee672013-03-28 06:21:22 +0000620 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
621 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000622 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000623 return 0;
624
625 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000626 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800627 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000628 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000629 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000630 return -EINVAL;
631
Vlad Yasevich407af322013-02-13 12:00:12 +0000632 if (p && protinfo) {
633 if (protinfo->nla_type & NLA_F_NESTED) {
634 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200635 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000636 if (err)
637 return err;
638
639 spin_lock_bh(&p->br->lock);
640 err = br_setport(p, tb);
641 spin_unlock_bh(&p->br->lock);
642 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800643 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000644 if (nla_len(protinfo) < sizeof(u8))
645 return -EINVAL;
646
647 spin_lock_bh(&p->br->lock);
648 err = br_set_port_state(p, nla_get_u8(protinfo));
649 spin_unlock_bh(&p->br->lock);
650 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000651 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000652 goto out;
653 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000654
Vlad Yasevich407af322013-02-13 12:00:12 +0000655 if (afspec) {
656 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
657 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000658 }
659
660 if (err == 0)
661 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000662out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000663 return err;
664}
665
Vlad Yasevich407af322013-02-13 12:00:12 +0000666/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800667int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000668{
Vlad Yasevich407af322013-02-13 12:00:12 +0000669 struct nlattr *afspec;
670 struct net_bridge_port *p;
Scott Feldman85080252015-05-10 09:48:03 -0700671 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000672
Hong zhi guoc60ee672013-03-28 06:21:22 +0000673 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000674 if (!afspec)
675 return 0;
676
677 p = br_port_get_rtnl(dev);
678 /* We want to accept dev as bridge itself as well */
679 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
680 return -EINVAL;
681
682 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
683 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800684 if (err == 0)
685 /* Send RTM_NEWLINK because userspace
686 * expects RTM_NEWLINK for vlan dels
687 */
688 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000689
690 return err;
691}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000692static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700693{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000694 if (tb[IFLA_ADDRESS]) {
695 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
696 return -EINVAL;
697 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
698 return -EADDRNOTAVAIL;
699 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700700
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900701 if (!data)
702 return 0;
703
704#ifdef CONFIG_BRIDGE_VLAN_FILTERING
705 if (data[IFLA_BR_VLAN_PROTOCOL]) {
706 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
707 case htons(ETH_P_8021Q):
708 case htons(ETH_P_8021AD):
709 break;
710 default:
711 return -EPROTONOSUPPORT;
712 }
713 }
714#endif
715
Thomas Graf32fe21c2007-03-22 11:59:03 -0700716 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700717}
718
Toshiaki Makita30313a32014-04-25 17:01:18 +0900719static int br_dev_newlink(struct net *src_net, struct net_device *dev,
720 struct nlattr *tb[], struct nlattr *data[])
721{
722 struct net_bridge *br = netdev_priv(dev);
723
724 if (tb[IFLA_ADDRESS]) {
725 spin_lock_bh(&br->lock);
726 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
727 spin_unlock_bh(&br->lock);
728 }
729
730 return register_netdevice(dev);
731}
732
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200733static int br_port_slave_changelink(struct net_device *brdev,
734 struct net_device *dev,
735 struct nlattr *tb[],
736 struct nlattr *data[])
737{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200738 struct net_bridge *br = netdev_priv(brdev);
739 int ret;
740
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200741 if (!data)
742 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200743
744 spin_lock_bh(&br->lock);
745 ret = br_setport(br_port_get_rtnl(dev), data);
746 spin_unlock_bh(&br->lock);
747
748 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200749}
750
Jiri Pirkoced82832014-09-05 15:51:29 +0200751static int br_port_fill_slave_info(struct sk_buff *skb,
752 const struct net_device *brdev,
753 const struct net_device *dev)
754{
755 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
756}
757
758static size_t br_port_get_slave_size(const struct net_device *brdev,
759 const struct net_device *dev)
760{
761 return br_port_info_size();
762}
763
Jiri Pirko13323512014-09-05 15:51:32 +0200764static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
765 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
766 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
767 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +0100768 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
769 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
770 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300771 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900772 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200773 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200774 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
775 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200776 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200777 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200778 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200779 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200780 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200781 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200782 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200783 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200784 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
785 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
786 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
787 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
788 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
789 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200790 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
791 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
792 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200793 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
Jiri Pirko13323512014-09-05 15:51:32 +0200794};
795
796static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
797 struct nlattr *data[])
798{
799 struct net_bridge *br = netdev_priv(brdev);
800 int err;
801
802 if (!data)
803 return 0;
804
805 if (data[IFLA_BR_FORWARD_DELAY]) {
806 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
807 if (err)
808 return err;
809 }
810
811 if (data[IFLA_BR_HELLO_TIME]) {
812 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
813 if (err)
814 return err;
815 }
816
817 if (data[IFLA_BR_MAX_AGE]) {
818 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
819 if (err)
820 return err;
821 }
822
Jörg Thalheimaf615762015-03-18 10:06:58 +0100823 if (data[IFLA_BR_AGEING_TIME]) {
824 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
825
826 br->ageing_time = clock_t_to_jiffies(ageing_time);
827 }
828
829 if (data[IFLA_BR_STP_STATE]) {
830 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
831
832 br_stp_set_enabled(br, stp_enabled);
833 }
834
835 if (data[IFLA_BR_PRIORITY]) {
836 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
837
838 br_stp_set_bridge_priority(br, priority);
839 }
840
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300841 if (data[IFLA_BR_VLAN_FILTERING]) {
842 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
843
844 err = __br_vlan_filter_toggle(br, vlan_filter);
845 if (err)
846 return err;
847 }
848
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900849#ifdef CONFIG_BRIDGE_VLAN_FILTERING
850 if (data[IFLA_BR_VLAN_PROTOCOL]) {
851 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
852
853 err = __br_vlan_set_proto(br, vlan_proto);
854 if (err)
855 return err;
856 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200857
858 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
859 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
860
861 err = __br_vlan_set_default_pvid(br, defpvid);
862 if (err)
863 return err;
864 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900865#endif
866
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200867 if (data[IFLA_BR_GROUP_FWD_MASK]) {
868 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
869
870 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
871 return -EINVAL;
872 br->group_fwd_mask = fwd_mask;
873 }
874
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200875 if (data[IFLA_BR_GROUP_ADDR]) {
876 u8 new_addr[ETH_ALEN];
877
878 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
879 return -EINVAL;
880 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
881 if (!is_link_local_ether_addr(new_addr))
882 return -EINVAL;
883 if (new_addr[5] == 1 || /* 802.3x Pause address */
884 new_addr[5] == 2 || /* 802.3ad Slow protocols */
885 new_addr[5] == 3) /* 802.1X PAE address */
886 return -EINVAL;
887 spin_lock_bh(&br->lock);
888 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
889 spin_unlock_bh(&br->lock);
890 br->group_addr_set = true;
891 br_recalculate_fwd_mask(br);
892 }
893
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +0200894 if (data[IFLA_BR_FDB_FLUSH])
895 br_fdb_flush(br);
896
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200897#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
898 if (data[IFLA_BR_MCAST_ROUTER]) {
899 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
900
901 err = br_multicast_set_router(br, multicast_router);
902 if (err)
903 return err;
904 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200905
906 if (data[IFLA_BR_MCAST_SNOOPING]) {
907 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
908
909 err = br_multicast_toggle(br, mcast_snooping);
910 if (err)
911 return err;
912 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200913
914 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
915 u8 val;
916
917 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
918 br->multicast_query_use_ifaddr = !!val;
919 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200920
921 if (data[IFLA_BR_MCAST_QUERIER]) {
922 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
923
924 err = br_multicast_set_querier(br, mcast_querier);
925 if (err)
926 return err;
927 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200928
929 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
930 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
931
932 br->hash_elasticity = val;
933 }
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200934
935 if (data[IFLA_BR_MCAST_HASH_MAX]) {
936 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
937
938 err = br_multicast_set_hash_max(br, hash_max);
939 if (err)
940 return err;
941 }
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200942
943 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
944 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
945
946 br->multicast_last_member_count = val;
947 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200948
949 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
950 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
951
952 br->multicast_startup_query_count = val;
953 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200954
955 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
956 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
957
958 br->multicast_last_member_interval = clock_t_to_jiffies(val);
959 }
960
961 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
962 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
963
964 br->multicast_membership_interval = clock_t_to_jiffies(val);
965 }
966
967 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
968 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
969
970 br->multicast_querier_interval = clock_t_to_jiffies(val);
971 }
972
973 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
974 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
975
976 br->multicast_query_interval = clock_t_to_jiffies(val);
977 }
978
979 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
980 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
981
982 br->multicast_query_response_interval = clock_t_to_jiffies(val);
983 }
984
985 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
986 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
987
988 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
989 }
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200990#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200991#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
992 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
993 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
994
995 br->nf_call_iptables = val ? true : false;
996 }
997
998 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
999 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1000
1001 br->nf_call_ip6tables = val ? true : false;
1002 }
1003
1004 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1005 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1006
1007 br->nf_call_arptables = val ? true : false;
1008 }
1009#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001010
Jiri Pirko13323512014-09-05 15:51:32 +02001011 return 0;
1012}
1013
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001014static size_t br_get_size(const struct net_device *brdev)
1015{
1016 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1017 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1018 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001019 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1020 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1021 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001022 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001023#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1024 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001025 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001026#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001027 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001028 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001029 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001030 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001031 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001032 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1033 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001034 nla_total_size(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1035 nla_total_size(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1036 nla_total_size(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1037 nla_total_size(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001038 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001039#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1040 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001041 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001042 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001043 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001044 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1045 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1046 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1047 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001048 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1049 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1050 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1051 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1052 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1053 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001054#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001055#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1056 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1057 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1058 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1059#endif
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001060 0;
1061}
1062
1063static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1064{
1065 struct net_bridge *br = netdev_priv(brdev);
1066 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1067 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1068 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001069 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1070 u32 stp_enabled = br->stp_enabled;
1071 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001072 u8 vlan_enabled = br_vlan_enabled(br);
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001073 u64 clockval;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001074
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001075 clockval = br_timer_value(&br->hello_timer);
1076 if (nla_put_u64(skb, IFLA_BR_HELLO_TIMER, clockval))
1077 return -EMSGSIZE;
1078 clockval = br_timer_value(&br->tcn_timer);
1079 if (nla_put_u64(skb, IFLA_BR_TCN_TIMER, clockval))
1080 return -EMSGSIZE;
1081 clockval = br_timer_value(&br->topology_change_timer);
1082 if (nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval))
1083 return -EMSGSIZE;
1084 clockval = br_timer_value(&br->gc_timer);
1085 if (nla_put_u64(skb, IFLA_BR_GC_TIMER, clockval))
1086 return -EMSGSIZE;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001087
1088 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1089 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001090 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1091 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1092 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001093 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001094 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001095 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1096 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1097 &br->bridge_id) ||
1098 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1099 &br->designated_root) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001100 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001101 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1102 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1103 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001104 br->topology_change_detected) ||
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001105 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001106 return -EMSGSIZE;
1107
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001108#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001109 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1110 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001111 return -EMSGSIZE;
1112#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001113#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001114 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001115 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1116 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001117 br->multicast_query_use_ifaddr) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001118 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1119 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001120 br->hash_elasticity) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001121 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1122 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001123 br->multicast_last_member_count) ||
1124 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1125 br->multicast_startup_query_count))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001126 return -EMSGSIZE;
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001127
1128 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1129 if (nla_put_u64(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval))
1130 return -EMSGSIZE;
1131 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1132 if (nla_put_u64(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval))
1133 return -EMSGSIZE;
1134 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1135 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval))
1136 return -EMSGSIZE;
1137 clockval = jiffies_to_clock_t(br->multicast_query_interval);
1138 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval))
1139 return -EMSGSIZE;
1140 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1141 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval))
1142 return -EMSGSIZE;
1143 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1144 if (nla_put_u64(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval))
1145 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001146#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001147#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1148 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1149 br->nf_call_iptables ? 1 : 0) ||
1150 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1151 br->nf_call_ip6tables ? 1 : 0) ||
1152 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1153 br->nf_call_arptables ? 1 : 0))
1154 return -EMSGSIZE;
1155#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001156
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001157 return 0;
1158}
1159
Roopa Prabhufed0a152015-02-25 23:55:40 -08001160static size_t br_get_link_af_size(const struct net_device *dev)
1161{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001162 struct net_bridge_port *p;
1163 struct net_bridge *br;
1164 int num_vlans = 0;
Roopa Prabhufed0a152015-02-25 23:55:40 -08001165
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001166 if (br_port_exists(dev)) {
1167 p = br_port_get_rtnl(dev);
1168 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +02001169 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001170 } else if (dev->priv_flags & IFF_EBRIDGE) {
1171 br = netdev_priv(dev);
1172 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +02001173 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001174 }
Roopa Prabhufed0a152015-02-25 23:55:40 -08001175
1176 /* Each VLAN is returned in bridge_vlan_info along with flags */
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +02001177 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhufed0a152015-02-25 23:55:40 -08001178}
1179
Daniel Borkmann207895fd32015-01-29 12:15:03 +01001180static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001181 .family = AF_BRIDGE,
1182 .get_link_af_size = br_get_link_af_size,
1183};
1184
stephen hemminger149ddd82012-06-26 05:48:45 +00001185struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001186 .kind = "bridge",
1187 .priv_size = sizeof(struct net_bridge),
1188 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001189 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001190 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001191 .validate = br_validate,
1192 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001193 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001194 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001195 .get_size = br_get_size,
1196 .fill_info = br_fill_info,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001197
1198 .slave_maxtype = IFLA_BRPORT_MAX,
1199 .slave_policy = br_port_policy,
1200 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001201 .get_slave_size = br_port_get_slave_size,
1202 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001203};
1204
1205int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001206{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001207 int err;
1208
1209 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001210 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001211
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001212 err = rtnl_link_register(&br_link_ops);
1213 if (err)
1214 goto out_af;
1215
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001216 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001217
1218out_af:
1219 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001220 br_mdb_uninit();
1221 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001222}
1223
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001224void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001225{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001226 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001227 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001228 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001229}