blob: fd37caf02b51a89ecc5e557e61328b7d5e6e26b1 [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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-09-25 19:00:11 +020078{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020079 int num_vlans;
80
Nikolay Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 Aleksandrov2594e902015-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 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000130 + 0;
131}
132
Roopa Prabhufed0a152015-02-25 23:55:40 -0800133static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800134{
135 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000136 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
137 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
138 + nla_total_size(4) /* IFLA_MASTER */
139 + nla_total_size(4) /* IFLA_MTU */
140 + nla_total_size(4) /* IFLA_LINK */
141 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800142 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800143 + nla_total_size(br_get_link_af_size_filtered(dev,
144 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000145}
146
147static int br_port_fill_attrs(struct sk_buff *skb,
148 const struct net_bridge_port *p)
149{
150 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
151
152 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
153 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
154 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000155 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000156 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
David S. Millerc2d3bab2012-12-05 16:24:45 -0500157 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400158 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400159 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Kyeyoon Park95850112014-10-23 14:49:17 -0700160 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200161 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
162 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
163 !!(p->flags & BR_PROXYARP_WIFI)))
stephen hemminger25c71c72012-11-13 07:53:05 +0000164 return -EMSGSIZE;
165
166 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800167}
168
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800169static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
170 u16 vid_end, u16 flags)
171{
172 struct bridge_vlan_info vinfo;
173
174 if ((vid_end - vid_start) > 0) {
175 /* add range to skb */
176 vinfo.vid = vid_start;
177 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
178 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
179 sizeof(vinfo), &vinfo))
180 goto nla_put_failure;
181
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800182 vinfo.vid = vid_end;
183 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
184 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
185 sizeof(vinfo), &vinfo))
186 goto nla_put_failure;
187 } else {
188 vinfo.vid = vid_start;
189 vinfo.flags = flags;
190 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
191 sizeof(vinfo), &vinfo))
192 goto nla_put_failure;
193 }
194
195 return 0;
196
197nla_put_failure:
198 return -EMSGSIZE;
199}
200
201static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200202 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800203{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200204 struct net_bridge_vlan *v;
205 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200206 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800207 int err = 0;
208
209 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
210 * and mark vlan info with begin and end flags
211 * if vlaninfo represents a range
212 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200213 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200214 list_for_each_entry(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800215 flags = 0;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200216 if (!br_vlan_should_use(v))
217 continue;
218 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800219 flags |= BRIDGE_VLAN_INFO_PVID;
220
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200221 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800222 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
223
224 if (vid_range_start == 0) {
225 goto initvars;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200226 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800227 flags == vid_range_flags) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200228 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800229 continue;
230 } else {
231 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
232 vid_range_end,
233 vid_range_flags);
234 if (err)
235 return err;
236 }
237
238initvars:
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200239 vid_range_start = v->vid;
240 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800241 vid_range_flags = flags;
242 }
243
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800244 if (vid_range_start != 0) {
245 /* Call it once more to send any left over vlans */
246 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
247 vid_range_end,
248 vid_range_flags);
249 if (err)
250 return err;
251 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800252
253 return 0;
254}
255
256static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200257 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800258{
259 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200260 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200261 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800262
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200263 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200264 list_for_each_entry(v, &vg->vlan_list, vlist) {
265 if (!br_vlan_should_use(v))
266 continue;
267
268 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800269 vinfo.flags = 0;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200270 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800271 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
272
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200273 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800274 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
275
276 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
277 sizeof(vinfo), &vinfo))
278 goto nla_put_failure;
279 }
280
281 return 0;
282
283nla_put_failure:
284 return -EMSGSIZE;
285}
286
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700287/*
288 * Create one netlink message for one interface
289 * Contains port and master info as well as carrier and bridge state.
290 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000291static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200292 struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000293 u32 pid, u32 seq, int event, unsigned int flags,
294 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700295{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200296 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800297 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700298 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700299 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700300
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000301 if (port)
302 br = port->br;
303 else
304 br = netdev_priv(dev);
305
stephen hemminger28a16c92010-05-10 09:31:09 +0000306 br_debug(br, "br_fill_info event %d port %s master %s\n",
307 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700308
Thomas Graf74685962006-11-20 16:20:22 -0800309 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
310 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800311 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700312
Thomas Graf74685962006-11-20 16:20:22 -0800313 hdr = nlmsg_data(nlh);
314 hdr->ifi_family = AF_BRIDGE;
315 hdr->__ifi_pad = 0;
316 hdr->ifi_type = dev->type;
317 hdr->ifi_index = dev->ifindex;
318 hdr->ifi_flags = dev_get_flags(dev);
319 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700320
David S. Miller2eb812e2012-04-01 20:49:54 -0400321 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
322 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
323 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
324 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
325 (dev->addr_len &&
326 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200327 (dev->ifindex != dev_get_iflink(dev) &&
328 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400329 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000330
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000331 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000332 struct nlattr *nest
333 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
334
335 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
336 goto nla_put_failure;
337 nla_nest_end(skb, nest);
338 }
339
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000340 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800341 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
342 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200343 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800344 struct nlattr *af;
345 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000346
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200347 if (port)
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200348 vg = nbp_vlan_group(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200349 else
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200350 vg = br_vlan_group(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000351
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200352 if (!vg || !vg->num_vlans)
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000353 goto done;
354
355 af = nla_nest_start(skb, IFLA_AF_SPEC);
356 if (!af)
357 goto nla_put_failure;
358
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800359 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200360 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800361 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200362 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800363 if (err)
364 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000365 nla_nest_end(skb, af);
366 }
367
368done:
Johannes Berg053c0952015-01-16 22:09:00 +0100369 nlmsg_end(skb, nlh);
370 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700371
Thomas Graf74685962006-11-20 16:20:22 -0800372nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800373 nlmsg_cancel(skb, nlh);
374 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700375}
376
377/*
378 * Notify listeners of a change in port information
379 */
380void br_ifinfo_notify(int event, struct net_bridge_port *port)
381{
Vlad Yasevich407af322013-02-13 12:00:12 +0000382 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700383 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700384 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800385 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700386
Vlad Yasevich407af322013-02-13 12:00:12 +0000387 if (!port)
388 return;
389
390 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000391 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000392 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000393
Roopa Prabhufed0a152015-02-25 23:55:40 -0800394 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700395 if (skb == NULL)
396 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700397
Roopa Prabhufed0a152015-02-25 23:55:40 -0800398 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800399 if (err < 0) {
400 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
401 WARN_ON(err == -EMSGSIZE);
402 kfree_skb(skb);
403 goto errout;
404 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800405 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
406 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700407errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800408 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700409}
410
Vlad Yasevich407af322013-02-13 12:00:12 +0000411
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700412/*
413 * Dump information about all ports, in response to GETLINK
414 */
John Fastabende5a55a82012-10-24 08:12:57 +0000415int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200416 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700417{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800418 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700419
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800420 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
421 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300422 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000423
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200424 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300425 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700426}
427
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800428static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
429 int cmd, struct bridge_vlan_info *vinfo)
430{
431 int err = 0;
432
433 switch (cmd) {
434 case RTM_SETLINK:
435 if (p) {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200436 /* if the MASTER flag is set this will act on the global
437 * per-VLAN entry as well
438 */
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800439 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
440 if (err)
441 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800442 } else {
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200443 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800444 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
445 }
446 break;
447
448 case RTM_DELLINK:
449 if (p) {
450 nbp_vlan_delete(p, vinfo->vid);
451 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
452 br_vlan_delete(p->br, vinfo->vid);
453 } else {
454 br_vlan_delete(br, vinfo->vid);
455 }
456 break;
457 }
458
459 return err;
460}
Vlad Yasevich407af322013-02-13 12:00:12 +0000461
462static int br_afspec(struct net_bridge *br,
463 struct net_bridge_port *p,
464 struct nlattr *af_spec,
465 int cmd)
466{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800467 struct bridge_vlan_info *vinfo_start = NULL;
468 struct bridge_vlan_info *vinfo = NULL;
469 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000470 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800471 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000472
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800473 nla_for_each_nested(attr, af_spec, rem) {
474 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
475 continue;
476 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000477 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800478 vinfo = nla_data(attr);
Nikolay Aleksandrov462e1ea2015-07-02 05:48:17 -0700479 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
480 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800481 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
482 if (vinfo_start)
483 return -EINVAL;
484 vinfo_start = vinfo;
485 continue;
486 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000487
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800488 if (vinfo_start) {
489 struct bridge_vlan_info tmp_vinfo;
490 int v;
491
492 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
493 return -EINVAL;
494
495 if (vinfo->vid <= vinfo_start->vid)
496 return -EINVAL;
497
498 memcpy(&tmp_vinfo, vinfo_start,
499 sizeof(struct bridge_vlan_info));
500
501 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
502 tmp_vinfo.vid = v;
503 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000504 if (err)
505 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800506 }
507 vinfo_start = NULL;
508 } else {
509 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000510 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800511 if (err)
512 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000513 }
514
515 return err;
516}
517
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200518static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000519 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
520 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
521 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
522 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000523 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000524 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100525 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400526 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400527 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200528 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200529 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000530};
531
532/* Change the state of the port and notify spanning tree */
533static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700534{
stephen hemminger25c71c72012-11-13 07:53:05 +0000535 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000536 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700537
538 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700539 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700540 return -EBUSY;
541
stephen hemminger576eb622012-12-28 18:15:22 +0000542 /* if device is not up, change is not allowed
543 * if link is not present, only allowable state is disabled
544 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000545 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000546 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700547 return -ENETDOWN;
548
Florian Fainelli775dd692014-09-30 16:13:19 -0700549 br_set_state(p, state);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700550 br_log_state(p);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000551 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700552 return 0;
553}
554
stephen hemminger25c71c72012-11-13 07:53:05 +0000555/* Set/clear or port flags based on attribute */
556static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
557 int attrtype, unsigned long mask)
558{
559 if (tb[attrtype]) {
560 u8 flag = nla_get_u8(tb[attrtype]);
561 if (flag)
562 p->flags |= mask;
563 else
564 p->flags &= ~mask;
565 }
566}
567
568/* Process bridge protocol info on port */
569static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
570{
571 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400572 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000573
574 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000575 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500576 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000577 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400578 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400579 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Kyeyoon Park95850112014-10-23 14:49:17 -0700580 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200581 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
stephen hemminger25c71c72012-11-13 07:53:05 +0000582
583 if (tb[IFLA_BRPORT_COST]) {
584 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
585 if (err)
586 return err;
587 }
588
589 if (tb[IFLA_BRPORT_PRIORITY]) {
590 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
591 if (err)
592 return err;
593 }
594
595 if (tb[IFLA_BRPORT_STATE]) {
596 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
597 if (err)
598 return err;
599 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400600
601 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000602 return 0;
603}
604
605/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800606int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000607{
stephen hemminger25c71c72012-11-13 07:53:05 +0000608 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000609 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000610 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000611 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Scott Feldman41c498b2015-05-10 09:47:59 -0700612 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000613
Hong zhi guoc60ee672013-03-28 06:21:22 +0000614 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
615 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000616 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000617 return 0;
618
619 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000620 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800621 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000622 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000623 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000624 return -EINVAL;
625
Vlad Yasevich407af322013-02-13 12:00:12 +0000626 if (p && protinfo) {
627 if (protinfo->nla_type & NLA_F_NESTED) {
628 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200629 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000630 if (err)
631 return err;
632
633 spin_lock_bh(&p->br->lock);
634 err = br_setport(p, tb);
635 spin_unlock_bh(&p->br->lock);
636 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800637 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000638 if (nla_len(protinfo) < sizeof(u8))
639 return -EINVAL;
640
641 spin_lock_bh(&p->br->lock);
642 err = br_set_port_state(p, nla_get_u8(protinfo));
643 spin_unlock_bh(&p->br->lock);
644 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000645 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000646 goto out;
647 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000648
Vlad Yasevich407af322013-02-13 12:00:12 +0000649 if (afspec) {
650 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
651 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000652 }
653
654 if (err == 0)
655 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000656out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000657 return err;
658}
659
Vlad Yasevich407af322013-02-13 12:00:12 +0000660/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800661int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000662{
Vlad Yasevich407af322013-02-13 12:00:12 +0000663 struct nlattr *afspec;
664 struct net_bridge_port *p;
Scott Feldman85080252015-05-10 09:48:03 -0700665 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000666
Hong zhi guoc60ee672013-03-28 06:21:22 +0000667 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000668 if (!afspec)
669 return 0;
670
671 p = br_port_get_rtnl(dev);
672 /* We want to accept dev as bridge itself as well */
673 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
674 return -EINVAL;
675
676 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
677 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800678 if (err == 0)
679 /* Send RTM_NEWLINK because userspace
680 * expects RTM_NEWLINK for vlan dels
681 */
682 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000683
684 return err;
685}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000686static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700687{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000688 if (tb[IFLA_ADDRESS]) {
689 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
690 return -EINVAL;
691 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
692 return -EADDRNOTAVAIL;
693 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700694
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900695 if (!data)
696 return 0;
697
698#ifdef CONFIG_BRIDGE_VLAN_FILTERING
699 if (data[IFLA_BR_VLAN_PROTOCOL]) {
700 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
701 case htons(ETH_P_8021Q):
702 case htons(ETH_P_8021AD):
703 break;
704 default:
705 return -EPROTONOSUPPORT;
706 }
707 }
708#endif
709
Thomas Graf32fe21c2007-03-22 11:59:03 -0700710 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700711}
712
Toshiaki Makita30313a32014-04-25 17:01:18 +0900713static int br_dev_newlink(struct net *src_net, struct net_device *dev,
714 struct nlattr *tb[], struct nlattr *data[])
715{
716 struct net_bridge *br = netdev_priv(dev);
717
718 if (tb[IFLA_ADDRESS]) {
719 spin_lock_bh(&br->lock);
720 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
721 spin_unlock_bh(&br->lock);
722 }
723
724 return register_netdevice(dev);
725}
726
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200727static int br_port_slave_changelink(struct net_device *brdev,
728 struct net_device *dev,
729 struct nlattr *tb[],
730 struct nlattr *data[])
731{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200732 struct net_bridge *br = netdev_priv(brdev);
733 int ret;
734
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200735 if (!data)
736 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200737
738 spin_lock_bh(&br->lock);
739 ret = br_setport(br_port_get_rtnl(dev), data);
740 spin_unlock_bh(&br->lock);
741
742 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200743}
744
Jiri Pirkoced82832014-09-05 15:51:29 +0200745static int br_port_fill_slave_info(struct sk_buff *skb,
746 const struct net_device *brdev,
747 const struct net_device *dev)
748{
749 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
750}
751
752static size_t br_port_get_slave_size(const struct net_device *brdev,
753 const struct net_device *dev)
754{
755 return br_port_info_size();
756}
757
Jiri Pirko13323512014-09-05 15:51:32 +0200758static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
759 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
760 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
761 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +0100762 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
763 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
764 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300765 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900766 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200767 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200768 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
769 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200770 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200771 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200772 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200773 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200774 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200775 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200776 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200777 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200778 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
779 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
780 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
781 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
782 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
783 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200784 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
785 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
786 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Jiri Pirko13323512014-09-05 15:51:32 +0200787};
788
789static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
790 struct nlattr *data[])
791{
792 struct net_bridge *br = netdev_priv(brdev);
793 int err;
794
795 if (!data)
796 return 0;
797
798 if (data[IFLA_BR_FORWARD_DELAY]) {
799 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
800 if (err)
801 return err;
802 }
803
804 if (data[IFLA_BR_HELLO_TIME]) {
805 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
806 if (err)
807 return err;
808 }
809
810 if (data[IFLA_BR_MAX_AGE]) {
811 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
812 if (err)
813 return err;
814 }
815
Jörg Thalheimaf615762015-03-18 10:06:58 +0100816 if (data[IFLA_BR_AGEING_TIME]) {
817 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
818
819 br->ageing_time = clock_t_to_jiffies(ageing_time);
820 }
821
822 if (data[IFLA_BR_STP_STATE]) {
823 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
824
825 br_stp_set_enabled(br, stp_enabled);
826 }
827
828 if (data[IFLA_BR_PRIORITY]) {
829 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
830
831 br_stp_set_bridge_priority(br, priority);
832 }
833
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300834 if (data[IFLA_BR_VLAN_FILTERING]) {
835 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
836
837 err = __br_vlan_filter_toggle(br, vlan_filter);
838 if (err)
839 return err;
840 }
841
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900842#ifdef CONFIG_BRIDGE_VLAN_FILTERING
843 if (data[IFLA_BR_VLAN_PROTOCOL]) {
844 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
845
846 err = __br_vlan_set_proto(br, vlan_proto);
847 if (err)
848 return err;
849 }
850#endif
851
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200852 if (data[IFLA_BR_GROUP_FWD_MASK]) {
853 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
854
855 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
856 return -EINVAL;
857 br->group_fwd_mask = fwd_mask;
858 }
859
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200860 if (data[IFLA_BR_GROUP_ADDR]) {
861 u8 new_addr[ETH_ALEN];
862
863 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
864 return -EINVAL;
865 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
866 if (!is_link_local_ether_addr(new_addr))
867 return -EINVAL;
868 if (new_addr[5] == 1 || /* 802.3x Pause address */
869 new_addr[5] == 2 || /* 802.3ad Slow protocols */
870 new_addr[5] == 3) /* 802.1X PAE address */
871 return -EINVAL;
872 spin_lock_bh(&br->lock);
873 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
874 spin_unlock_bh(&br->lock);
875 br->group_addr_set = true;
876 br_recalculate_fwd_mask(br);
877 }
878
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +0200879 if (data[IFLA_BR_FDB_FLUSH])
880 br_fdb_flush(br);
881
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200882#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
883 if (data[IFLA_BR_MCAST_ROUTER]) {
884 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
885
886 err = br_multicast_set_router(br, multicast_router);
887 if (err)
888 return err;
889 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200890
891 if (data[IFLA_BR_MCAST_SNOOPING]) {
892 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
893
894 err = br_multicast_toggle(br, mcast_snooping);
895 if (err)
896 return err;
897 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200898
899 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
900 u8 val;
901
902 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
903 br->multicast_query_use_ifaddr = !!val;
904 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200905
906 if (data[IFLA_BR_MCAST_QUERIER]) {
907 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
908
909 err = br_multicast_set_querier(br, mcast_querier);
910 if (err)
911 return err;
912 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200913
914 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
915 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
916
917 br->hash_elasticity = val;
918 }
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200919
920 if (data[IFLA_BR_MCAST_HASH_MAX]) {
921 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
922
923 err = br_multicast_set_hash_max(br, hash_max);
924 if (err)
925 return err;
926 }
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200927
928 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
929 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
930
931 br->multicast_last_member_count = val;
932 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200933
934 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
935 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
936
937 br->multicast_startup_query_count = val;
938 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200939
940 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
941 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
942
943 br->multicast_last_member_interval = clock_t_to_jiffies(val);
944 }
945
946 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
947 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
948
949 br->multicast_membership_interval = clock_t_to_jiffies(val);
950 }
951
952 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
953 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
954
955 br->multicast_querier_interval = clock_t_to_jiffies(val);
956 }
957
958 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
959 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
960
961 br->multicast_query_interval = clock_t_to_jiffies(val);
962 }
963
964 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
965 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
966
967 br->multicast_query_response_interval = clock_t_to_jiffies(val);
968 }
969
970 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
971 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
972
973 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
974 }
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200975#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200976#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
977 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
978 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
979
980 br->nf_call_iptables = val ? true : false;
981 }
982
983 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
984 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
985
986 br->nf_call_ip6tables = val ? true : false;
987 }
988
989 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
990 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
991
992 br->nf_call_arptables = val ? true : false;
993 }
994#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200995
Jiri Pirko13323512014-09-05 15:51:32 +0200996 return 0;
997}
998
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +0200999static size_t br_get_size(const struct net_device *brdev)
1000{
1001 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1002 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1003 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001004 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1005 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1006 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001007 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001008#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1009 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1010#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001011 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001012 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001013 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001014 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001015 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001016 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1017 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001018 nla_total_size(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1019 nla_total_size(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1020 nla_total_size(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1021 nla_total_size(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001022 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001023#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1024 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001025 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001026 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001027 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001028 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1029 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1030 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1031 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001032 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1033 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1034 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1035 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1036 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1037 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001038#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001039#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1040 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1041 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1042 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1043#endif
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001044 0;
1045}
1046
1047static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1048{
1049 struct net_bridge *br = netdev_priv(brdev);
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001050 u64 hello_timer, tcn_timer, topology_change_timer, gc_timer, clockval;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001051 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1052 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1053 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001054 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1055 u32 stp_enabled = br->stp_enabled;
1056 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001057 u16 group_fwd_mask = br->group_fwd_mask;
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001058 u8 vlan_enabled = br_vlan_enabled(br);
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001059 struct ifla_bridge_id root_id, bridge_id;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001060
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001061 memset(&bridge_id, 0, sizeof(bridge_id));
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001062 memset(&root_id, 0, sizeof(root_id));
1063 memcpy(root_id.prio, br->designated_root.prio, sizeof(root_id.prio));
1064 memcpy(root_id.addr, br->designated_root.addr, sizeof(root_id.addr));
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001065 memcpy(bridge_id.prio, br->bridge_id.prio, sizeof(bridge_id.prio));
1066 memcpy(bridge_id.addr, br->bridge_id.addr, sizeof(bridge_id.addr));
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001067 hello_timer = br_timer_value(&br->hello_timer);
1068 tcn_timer = br_timer_value(&br->tcn_timer);
1069 topology_change_timer = br_timer_value(&br->topology_change_timer);
1070 gc_timer = br_timer_value(&br->gc_timer);
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001071 clockval = 0;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001072
1073 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1074 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001075 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1076 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1077 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001078 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001079 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001080 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, group_fwd_mask) ||
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001081 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(root_id), &root_id) ||
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001082 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(bridge_id), &bridge_id) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001083 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001084 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1085 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1086 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001087 br->topology_change_detected) ||
1088 nla_put_u64(skb, IFLA_BR_HELLO_TIMER, hello_timer) ||
1089 nla_put_u64(skb, IFLA_BR_TCN_TIMER, tcn_timer) ||
1090 nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER,
1091 topology_change_timer) ||
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001092 nla_put_u64(skb, IFLA_BR_GC_TIMER, gc_timer) ||
1093 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001094 return -EMSGSIZE;
1095
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001096#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1097 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto))
1098 return -EMSGSIZE;
1099#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001100#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001101 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001102 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1103 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001104 br->multicast_query_use_ifaddr) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001105 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1106 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001107 br->hash_elasticity) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001108 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1109 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001110 br->multicast_last_member_count) ||
1111 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1112 br->multicast_startup_query_count))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001113 return -EMSGSIZE;
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001114
1115 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1116 if (nla_put_u64(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval))
1117 return -EMSGSIZE;
1118 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1119 if (nla_put_u64(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval))
1120 return -EMSGSIZE;
1121 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1122 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval))
1123 return -EMSGSIZE;
1124 clockval = jiffies_to_clock_t(br->multicast_query_interval);
1125 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval))
1126 return -EMSGSIZE;
1127 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1128 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval))
1129 return -EMSGSIZE;
1130 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1131 if (nla_put_u64(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval))
1132 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001133#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001134#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1135 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1136 br->nf_call_iptables ? 1 : 0) ||
1137 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1138 br->nf_call_ip6tables ? 1 : 0) ||
1139 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1140 br->nf_call_arptables ? 1 : 0))
1141 return -EMSGSIZE;
1142#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001143
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001144 return 0;
1145}
1146
Roopa Prabhufed0a152015-02-25 23:55:40 -08001147static size_t br_get_link_af_size(const struct net_device *dev)
1148{
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001149 struct net_bridge_port *p;
1150 struct net_bridge *br;
1151 int num_vlans = 0;
Roopa Prabhufed0a152015-02-25 23:55:40 -08001152
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001153 if (br_port_exists(dev)) {
1154 p = br_port_get_rtnl(dev);
1155 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +02001156 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001157 } else if (dev->priv_flags & IFF_EBRIDGE) {
1158 br = netdev_priv(dev);
1159 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +02001160 RTEXT_FILTER_BRVLAN);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001161 }
Roopa Prabhufed0a152015-02-25 23:55:40 -08001162
1163 /* Each VLAN is returned in bridge_vlan_info along with flags */
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +02001164 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
Roopa Prabhufed0a152015-02-25 23:55:40 -08001165}
1166
Daniel Borkmann207895f2015-01-29 12:15:03 +01001167static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001168 .family = AF_BRIDGE,
1169 .get_link_af_size = br_get_link_af_size,
1170};
1171
stephen hemminger149ddd82012-06-26 05:48:45 +00001172struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001173 .kind = "bridge",
1174 .priv_size = sizeof(struct net_bridge),
1175 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001176 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001177 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001178 .validate = br_validate,
1179 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001180 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001181 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001182 .get_size = br_get_size,
1183 .fill_info = br_fill_info,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001184
1185 .slave_maxtype = IFLA_BRPORT_MAX,
1186 .slave_policy = br_port_policy,
1187 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001188 .get_slave_size = br_port_get_slave_size,
1189 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001190};
1191
1192int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001193{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001194 int err;
1195
1196 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001197 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001198
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001199 err = rtnl_link_register(&br_link_ops);
1200 if (err)
1201 goto out_af;
1202
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001203 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001204
1205out_af:
1206 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001207 br_mdb_uninit();
1208 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001209}
1210
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001211void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001212{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001213 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001214 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001215 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001216}