blob: 6c087cd049b91af1d99ccf5a72d8cc5e58409c30 [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);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200105 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200106 } else if (dev->priv_flags & IFF_EBRIDGE) {
107 br = netdev_priv(dev);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200108 vg = br_vlan_group_rcu(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 */
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100126 + nla_total_size(1) /* IFLA_BRPORT_MCAST_TO_UCAST */
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400127 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400128 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200129 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200130 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200131 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200132 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200133 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
134 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200135 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
136 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200137 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
138 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200139 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
140 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
141 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200142#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
143 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
144#endif
stephen hemminger25c71c72012-11-13 07:53:05 +0000145 + 0;
146}
147
Roopa Prabhufed0a152015-02-25 23:55:40 -0800148static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800149{
150 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000151 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
152 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
153 + nla_total_size(4) /* IFLA_MASTER */
154 + nla_total_size(4) /* IFLA_MTU */
155 + nla_total_size(4) /* IFLA_LINK */
156 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800157 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800158 + nla_total_size(br_get_link_af_size_filtered(dev,
159 filter_mask)); /* IFLA_AF_SPEC */
stephen hemminger25c71c72012-11-13 07:53:05 +0000160}
161
162static int br_port_fill_attrs(struct sk_buff *skb,
163 const struct net_bridge_port *p)
164{
165 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200166 u64 timerval;
stephen hemminger25c71c72012-11-13 07:53:05 +0000167
168 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
169 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
170 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000171 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000172 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200173 nla_put_u8(skb, IFLA_BRPORT_PROTECT,
174 !!(p->flags & BR_ROOT_BLOCK)) ||
175 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
176 !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100177 nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
178 !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400179 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200180 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
181 !!(p->flags & BR_FLOOD)) ||
182 nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
183 !!(p->flags & BR_MCAST_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200184 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
185 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200186 !!(p->flags & BR_PROXYARP_WIFI)) ||
187 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200188 &p->designated_root) ||
189 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200190 &p->designated_bridge) ||
191 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200192 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
193 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200194 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
195 nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
196 p->topology_change_ack) ||
197 nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending))
stephen hemminger25c71c72012-11-13 07:53:05 +0000198 return -EMSGSIZE;
199
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200200 timerval = br_timer_value(&p->message_age_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200201 if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
202 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200203 return -EMSGSIZE;
204 timerval = br_timer_value(&p->forward_delay_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200205 if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
206 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200207 return -EMSGSIZE;
208 timerval = br_timer_value(&p->hold_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200209 if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
210 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200211 return -EMSGSIZE;
212
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200213#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
214 if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
215 p->multicast_router))
216 return -EMSGSIZE;
217#endif
218
stephen hemminger25c71c72012-11-13 07:53:05 +0000219 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800220}
221
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800222static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
223 u16 vid_end, u16 flags)
224{
225 struct bridge_vlan_info vinfo;
226
227 if ((vid_end - vid_start) > 0) {
228 /* add range to skb */
229 vinfo.vid = vid_start;
230 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
231 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
232 sizeof(vinfo), &vinfo))
233 goto nla_put_failure;
234
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800235 vinfo.vid = vid_end;
236 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
237 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
238 sizeof(vinfo), &vinfo))
239 goto nla_put_failure;
240 } else {
241 vinfo.vid = vid_start;
242 vinfo.flags = flags;
243 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
244 sizeof(vinfo), &vinfo))
245 goto nla_put_failure;
246 }
247
248 return 0;
249
250nla_put_failure:
251 return -EMSGSIZE;
252}
253
254static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200255 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800256{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200257 struct net_bridge_vlan *v;
258 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200259 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800260 int err = 0;
261
262 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
263 * and mark vlan info with begin and end flags
264 * if vlaninfo represents a range
265 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200266 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200267 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800268 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200269 if (!br_vlan_should_use(v))
270 continue;
271 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800272 flags |= BRIDGE_VLAN_INFO_PVID;
273
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200274 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800275 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
276
277 if (vid_range_start == 0) {
278 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200279 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800280 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200281 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800282 continue;
283 } else {
284 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
285 vid_range_end,
286 vid_range_flags);
287 if (err)
288 return err;
289 }
290
291initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200292 vid_range_start = v->vid;
293 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800294 vid_range_flags = flags;
295 }
296
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800297 if (vid_range_start != 0) {
298 /* Call it once more to send any left over vlans */
299 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
300 vid_range_end,
301 vid_range_flags);
302 if (err)
303 return err;
304 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800305
306 return 0;
307}
308
309static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200310 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800311{
312 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200313 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200314 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800315
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200316 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200317 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200318 if (!br_vlan_should_use(v))
319 continue;
320
321 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800322 vinfo.flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200323 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800324 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
325
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200326 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800327 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
328
329 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
330 sizeof(vinfo), &vinfo))
331 goto nla_put_failure;
332 }
333
334 return 0;
335
336nla_put_failure:
337 return -EMSGSIZE;
338}
339
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700340/*
341 * Create one netlink message for one interface
342 * Contains port and master info as well as carrier and bridge state.
343 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000344static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200345 struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000346 u32 pid, u32 seq, int event, unsigned int flags,
347 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700348{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200349 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800350 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700351 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700352 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700353
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000354 if (port)
355 br = port->br;
356 else
357 br = netdev_priv(dev);
358
stephen hemminger28a16c92010-05-10 09:31:09 +0000359 br_debug(br, "br_fill_info event %d port %s master %s\n",
360 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700361
Thomas Graf74685962006-11-20 16:20:22 -0800362 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
363 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800364 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700365
Thomas Graf74685962006-11-20 16:20:22 -0800366 hdr = nlmsg_data(nlh);
367 hdr->ifi_family = AF_BRIDGE;
368 hdr->__ifi_pad = 0;
369 hdr->ifi_type = dev->type;
370 hdr->ifi_index = dev->ifindex;
371 hdr->ifi_flags = dev_get_flags(dev);
372 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700373
David S. Miller2eb812e2012-04-01 20:49:54 -0400374 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
375 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
376 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
377 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
378 (dev->addr_len &&
379 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200380 (dev->ifindex != dev_get_iflink(dev) &&
381 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400382 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000383
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000384 if (event == RTM_NEWLINK && port) {
stephen hemminger25c71c72012-11-13 07:53:05 +0000385 struct nlattr *nest
386 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
387
388 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
389 goto nla_put_failure;
390 nla_nest_end(skb, nest);
391 }
392
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000393 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800394 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
395 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200396 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800397 struct nlattr *af;
398 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000399
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200400 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
401 rcu_read_lock();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200402 if (port)
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200403 vg = nbp_vlan_group_rcu(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200404 else
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200405 vg = br_vlan_group_rcu(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000406
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200407 if (!vg || !vg->num_vlans) {
408 rcu_read_unlock();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000409 goto done;
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200410 }
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000411 af = nla_nest_start(skb, IFLA_AF_SPEC);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200412 if (!af) {
413 rcu_read_unlock();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000414 goto nla_put_failure;
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200415 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800416 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200417 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800418 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200419 err = br_fill_ifvlaninfo(skb, vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200420 rcu_read_unlock();
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800421 if (err)
422 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000423 nla_nest_end(skb, af);
424 }
425
426done:
Johannes Berg053c0952015-01-16 22:09:00 +0100427 nlmsg_end(skb, nlh);
428 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700429
Thomas Graf74685962006-11-20 16:20:22 -0800430nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800431 nlmsg_cancel(skb, nlh);
432 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700433}
434
435/*
436 * Notify listeners of a change in port information
437 */
438void br_ifinfo_notify(int event, struct net_bridge_port *port)
439{
Vlad Yasevich407af322013-02-13 12:00:12 +0000440 struct net *net;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700441 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700442 int err = -ENOBUFS;
Roopa Prabhufed0a152015-02-25 23:55:40 -0800443 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700444
Vlad Yasevich407af322013-02-13 12:00:12 +0000445 if (!port)
446 return;
447
448 net = dev_net(port->dev);
stephen hemminger28a16c92010-05-10 09:31:09 +0000449 br_debug(port->br, "port %u(%s) event %d\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000450 (unsigned int)port->port_no, port->dev->name, event);
stephen hemminger28a16c92010-05-10 09:31:09 +0000451
Roopa Prabhufed0a152015-02-25 23:55:40 -0800452 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700453 if (skb == NULL)
454 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700455
Roopa Prabhufed0a152015-02-25 23:55:40 -0800456 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800457 if (err < 0) {
458 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
459 WARN_ON(err == -EMSGSIZE);
460 kfree_skb(skb);
461 goto errout;
462 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800463 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
464 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700465errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800466 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700467}
468
Vlad Yasevich407af322013-02-13 12:00:12 +0000469
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700470/*
471 * Dump information about all ports, in response to GETLINK
472 */
John Fastabende5a55a82012-10-24 08:12:57 +0000473int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200474 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700475{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800476 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700477
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800478 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
479 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300480 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000481
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200482 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300483 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700484}
485
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800486static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
487 int cmd, struct bridge_vlan_info *vinfo)
488{
489 int err = 0;
490
491 switch (cmd) {
492 case RTM_SETLINK:
493 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200494 /* if the MASTER flag is set this will act on the global
495 * per-VLAN entry as well
496 */
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800497 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
498 if (err)
499 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800500 } else {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200501 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800502 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
503 }
504 break;
505
506 case RTM_DELLINK:
507 if (p) {
508 nbp_vlan_delete(p, vinfo->vid);
509 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
510 br_vlan_delete(p->br, vinfo->vid);
511 } else {
512 br_vlan_delete(br, vinfo->vid);
513 }
514 break;
515 }
516
517 return err;
518}
Vlad Yasevich407af322013-02-13 12:00:12 +0000519
520static int br_afspec(struct net_bridge *br,
521 struct net_bridge_port *p,
522 struct nlattr *af_spec,
523 int cmd)
524{
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800525 struct bridge_vlan_info *vinfo_start = NULL;
526 struct bridge_vlan_info *vinfo = NULL;
527 struct nlattr *attr;
Vlad Yasevich407af322013-02-13 12:00:12 +0000528 int err = 0;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800529 int rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000530
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800531 nla_for_each_nested(attr, af_spec, rem) {
532 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
533 continue;
534 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
Vlad Yasevich407af322013-02-13 12:00:12 +0000535 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800536 vinfo = nla_data(attr);
Nikolay Aleksandrov462e1ea2015-07-02 05:48:17 -0700537 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
538 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800539 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
540 if (vinfo_start)
541 return -EINVAL;
542 vinfo_start = vinfo;
Nikolay Aleksandrov6623c602015-10-11 12:49:56 +0200543 /* don't allow range of pvids */
544 if (vinfo_start->flags & BRIDGE_VLAN_INFO_PVID)
545 return -EINVAL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800546 continue;
547 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000548
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800549 if (vinfo_start) {
550 struct bridge_vlan_info tmp_vinfo;
551 int v;
552
553 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
554 return -EINVAL;
555
556 if (vinfo->vid <= vinfo_start->vid)
557 return -EINVAL;
558
559 memcpy(&tmp_vinfo, vinfo_start,
560 sizeof(struct bridge_vlan_info));
561
562 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
563 tmp_vinfo.vid = v;
564 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000565 if (err)
566 break;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800567 }
568 vinfo_start = NULL;
569 } else {
570 err = br_vlan_info(br, p, cmd, vinfo);
Vlad Yasevich407af322013-02-13 12:00:12 +0000571 }
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800572 if (err)
573 break;
Vlad Yasevich407af322013-02-13 12:00:12 +0000574 }
575
576 return err;
577}
578
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200579static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000580 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
581 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
582 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
583 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000584 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000585 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100586 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400587 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400588 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200589 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200590 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200591 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100592 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000593};
594
595/* Change the state of the port and notify spanning tree */
596static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700597{
stephen hemminger25c71c72012-11-13 07:53:05 +0000598 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000599 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700600
601 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700602 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700603 return -EBUSY;
604
stephen hemminger576eb622012-12-28 18:15:22 +0000605 /* if device is not up, change is not allowed
606 * if link is not present, only allowable state is disabled
607 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000608 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000609 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700610 return -ENETDOWN;
611
Florian Fainelli775dd692014-09-30 16:13:19 -0700612 br_set_state(p, state);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000613 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700614 return 0;
615}
616
stephen hemminger25c71c72012-11-13 07:53:05 +0000617/* Set/clear or port flags based on attribute */
618static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
619 int attrtype, unsigned long mask)
620{
621 if (tb[attrtype]) {
622 u8 flag = nla_get_u8(tb[attrtype]);
623 if (flag)
624 p->flags |= mask;
625 else
626 p->flags &= ~mask;
627 }
628}
629
630/* Process bridge protocol info on port */
631static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
632{
633 int err;
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400634 unsigned long old_flags = p->flags;
stephen hemminger25c71c72012-11-13 07:53:05 +0000635
636 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
stephen hemmingera2e01a62012-11-13 07:53:07 +0000637 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
David S. Millerc2d3bab2012-12-05 16:24:45 -0500638 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
Vlad Yasevich3d84fa92013-03-15 06:39:12 +0000639 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400640 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
Vlad Yasevich867a5942013-06-05 10:08:01 -0400641 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200642 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100643 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
Kyeyoon Park95850112014-10-23 14:49:17 -0700644 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200645 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
stephen hemminger25c71c72012-11-13 07:53:05 +0000646
647 if (tb[IFLA_BRPORT_COST]) {
648 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
649 if (err)
650 return err;
651 }
652
653 if (tb[IFLA_BRPORT_PRIORITY]) {
654 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
655 if (err)
656 return err;
657 }
658
659 if (tb[IFLA_BRPORT_STATE]) {
660 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
661 if (err)
662 return err;
663 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400664
Nikolay Aleksandrov9b0c6e42015-10-06 14:12:01 +0200665 if (tb[IFLA_BRPORT_FLUSH])
666 br_fdb_delete_by_port(p->br, p, 0, 0);
667
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200668#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
669 if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
670 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
671
672 err = br_multicast_set_port_router(p, mcast_router);
673 if (err)
674 return err;
675 }
676#endif
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400677 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000678 return 0;
679}
680
681/* Change state and parameters on port. */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800682int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
stephen hemminger25c71c72012-11-13 07:53:05 +0000683{
stephen hemminger25c71c72012-11-13 07:53:05 +0000684 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000685 struct nlattr *afspec;
stephen hemminger25c71c72012-11-13 07:53:05 +0000686 struct net_bridge_port *p;
Dan Carpenter2062cc22012-12-07 01:10:46 +0000687 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
Scott Feldman41c498b2015-05-10 09:47:59 -0700688 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000689
Hong zhi guoc60ee672013-03-28 06:21:22 +0000690 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
691 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000692 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000693 return 0;
694
695 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000696 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800697 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000698 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000699 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000700 return -EINVAL;
701
Vlad Yasevich407af322013-02-13 12:00:12 +0000702 if (p && protinfo) {
703 if (protinfo->nla_type & NLA_F_NESTED) {
704 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200705 protinfo, br_port_policy);
Vlad Yasevich407af322013-02-13 12:00:12 +0000706 if (err)
707 return err;
708
709 spin_lock_bh(&p->br->lock);
710 err = br_setport(p, tb);
711 spin_unlock_bh(&p->br->lock);
712 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800713 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000714 if (nla_len(protinfo) < sizeof(u8))
715 return -EINVAL;
716
717 spin_lock_bh(&p->br->lock);
718 err = br_set_port_state(p, nla_get_u8(protinfo));
719 spin_unlock_bh(&p->br->lock);
720 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000721 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000722 goto out;
723 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000724
Vlad Yasevich407af322013-02-13 12:00:12 +0000725 if (afspec) {
726 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
727 afspec, RTM_SETLINK);
stephen hemminger25c71c72012-11-13 07:53:05 +0000728 }
729
730 if (err == 0)
731 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000732out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000733 return err;
734}
735
Vlad Yasevich407af322013-02-13 12:00:12 +0000736/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800737int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000738{
Vlad Yasevich407af322013-02-13 12:00:12 +0000739 struct nlattr *afspec;
740 struct net_bridge_port *p;
Scott Feldman85080252015-05-10 09:48:03 -0700741 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000742
Hong zhi guoc60ee672013-03-28 06:21:22 +0000743 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000744 if (!afspec)
745 return 0;
746
747 p = br_port_get_rtnl(dev);
748 /* We want to accept dev as bridge itself as well */
749 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
750 return -EINVAL;
751
752 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
753 afspec, RTM_DELLINK);
Roopa Prabhu02dba432015-01-14 20:02:25 -0800754 if (err == 0)
755 /* Send RTM_NEWLINK because userspace
756 * expects RTM_NEWLINK for vlan dels
757 */
758 br_ifinfo_notify(RTM_NEWLINK, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000759
760 return err;
761}
stephen hemmingerbb900b22011-04-04 14:03:32 +0000762static int br_validate(struct nlattr *tb[], struct nlattr *data[])
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700763{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000764 if (tb[IFLA_ADDRESS]) {
765 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
766 return -EINVAL;
767 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
768 return -EADDRNOTAVAIL;
769 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700770
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900771 if (!data)
772 return 0;
773
774#ifdef CONFIG_BRIDGE_VLAN_FILTERING
775 if (data[IFLA_BR_VLAN_PROTOCOL]) {
776 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
777 case htons(ETH_P_8021Q):
778 case htons(ETH_P_8021AD):
779 break;
780 default:
781 return -EPROTONOSUPPORT;
782 }
783 }
784#endif
785
Thomas Graf32fe21c2007-03-22 11:59:03 -0700786 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700787}
788
Toshiaki Makita30313a32014-04-25 17:01:18 +0900789static int br_dev_newlink(struct net *src_net, struct net_device *dev,
790 struct nlattr *tb[], struct nlattr *data[])
791{
792 struct net_bridge *br = netdev_priv(dev);
793
794 if (tb[IFLA_ADDRESS]) {
795 spin_lock_bh(&br->lock);
796 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
797 spin_unlock_bh(&br->lock);
798 }
799
800 return register_netdevice(dev);
801}
802
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200803static int br_port_slave_changelink(struct net_device *brdev,
804 struct net_device *dev,
805 struct nlattr *tb[],
806 struct nlattr *data[])
807{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200808 struct net_bridge *br = netdev_priv(brdev);
809 int ret;
810
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200811 if (!data)
812 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +0200813
814 spin_lock_bh(&br->lock);
815 ret = br_setport(br_port_get_rtnl(dev), data);
816 spin_unlock_bh(&br->lock);
817
818 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200819}
820
Jiri Pirkoced82832014-09-05 15:51:29 +0200821static int br_port_fill_slave_info(struct sk_buff *skb,
822 const struct net_device *brdev,
823 const struct net_device *dev)
824{
825 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
826}
827
828static size_t br_port_get_slave_size(const struct net_device *brdev,
829 const struct net_device *dev)
830{
831 return br_port_info_size();
832}
833
Jiri Pirko13323512014-09-05 15:51:32 +0200834static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
835 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
836 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
837 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +0100838 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
839 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
840 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300841 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900842 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200843 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200844 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
845 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200846 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200847 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200848 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +0200849 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +0200850 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +0200851 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +0200852 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +0200853 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +0200854 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
855 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
856 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
857 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
858 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
859 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +0200860 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
861 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
862 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200863 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200864 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200865 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +0100866 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +0100867 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
Jiri Pirko13323512014-09-05 15:51:32 +0200868};
869
870static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
871 struct nlattr *data[])
872{
873 struct net_bridge *br = netdev_priv(brdev);
874 int err;
875
876 if (!data)
877 return 0;
878
879 if (data[IFLA_BR_FORWARD_DELAY]) {
880 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
881 if (err)
882 return err;
883 }
884
885 if (data[IFLA_BR_HELLO_TIME]) {
886 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
887 if (err)
888 return err;
889 }
890
891 if (data[IFLA_BR_MAX_AGE]) {
892 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
893 if (err)
894 return err;
895 }
896
Jörg Thalheimaf615762015-03-18 10:06:58 +0100897 if (data[IFLA_BR_AGEING_TIME]) {
Scott Feldmanc62987b2015-10-08 19:23:19 -0700898 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
899 if (err)
900 return err;
Jörg Thalheimaf615762015-03-18 10:06:58 +0100901 }
902
903 if (data[IFLA_BR_STP_STATE]) {
904 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
905
906 br_stp_set_enabled(br, stp_enabled);
907 }
908
909 if (data[IFLA_BR_PRIORITY]) {
910 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
911
912 br_stp_set_bridge_priority(br, priority);
913 }
914
Nikolay Aleksandrova7854032015-08-07 19:40:45 +0300915 if (data[IFLA_BR_VLAN_FILTERING]) {
916 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
917
918 err = __br_vlan_filter_toggle(br, vlan_filter);
919 if (err)
920 return err;
921 }
922
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900923#ifdef CONFIG_BRIDGE_VLAN_FILTERING
924 if (data[IFLA_BR_VLAN_PROTOCOL]) {
925 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
926
927 err = __br_vlan_set_proto(br, vlan_proto);
928 if (err)
929 return err;
930 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +0200931
932 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
933 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
934
935 err = __br_vlan_set_default_pvid(br, defpvid);
936 if (err)
937 return err;
938 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +0200939
940 if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
941 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
942
943 err = br_vlan_set_stats(br, vlan_stats);
944 if (err)
945 return err;
946 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900947#endif
948
Nikolay Aleksandrov79102282015-10-04 14:23:28 +0200949 if (data[IFLA_BR_GROUP_FWD_MASK]) {
950 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
951
952 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
953 return -EINVAL;
954 br->group_fwd_mask = fwd_mask;
955 }
956
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +0200957 if (data[IFLA_BR_GROUP_ADDR]) {
958 u8 new_addr[ETH_ALEN];
959
960 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
961 return -EINVAL;
962 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
963 if (!is_link_local_ether_addr(new_addr))
964 return -EINVAL;
965 if (new_addr[5] == 1 || /* 802.3x Pause address */
966 new_addr[5] == 2 || /* 802.3ad Slow protocols */
967 new_addr[5] == 3) /* 802.1X PAE address */
968 return -EINVAL;
969 spin_lock_bh(&br->lock);
970 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
971 spin_unlock_bh(&br->lock);
972 br->group_addr_set = true;
973 br_recalculate_fwd_mask(br);
974 }
975
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +0200976 if (data[IFLA_BR_FDB_FLUSH])
977 br_fdb_flush(br);
978
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +0200979#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
980 if (data[IFLA_BR_MCAST_ROUTER]) {
981 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
982
983 err = br_multicast_set_router(br, multicast_router);
984 if (err)
985 return err;
986 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +0200987
988 if (data[IFLA_BR_MCAST_SNOOPING]) {
989 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
990
991 err = br_multicast_toggle(br, mcast_snooping);
992 if (err)
993 return err;
994 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +0200995
996 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
997 u8 val;
998
999 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1000 br->multicast_query_use_ifaddr = !!val;
1001 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001002
1003 if (data[IFLA_BR_MCAST_QUERIER]) {
1004 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1005
1006 err = br_multicast_set_querier(br, mcast_querier);
1007 if (err)
1008 return err;
1009 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001010
1011 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
1012 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
1013
1014 br->hash_elasticity = val;
1015 }
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001016
1017 if (data[IFLA_BR_MCAST_HASH_MAX]) {
1018 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1019
1020 err = br_multicast_set_hash_max(br, hash_max);
1021 if (err)
1022 return err;
1023 }
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001024
1025 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1026 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1027
1028 br->multicast_last_member_count = val;
1029 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001030
1031 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1032 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1033
1034 br->multicast_startup_query_count = val;
1035 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001036
1037 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1038 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1039
1040 br->multicast_last_member_interval = clock_t_to_jiffies(val);
1041 }
1042
1043 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1044 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1045
1046 br->multicast_membership_interval = clock_t_to_jiffies(val);
1047 }
1048
1049 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1050 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1051
1052 br->multicast_querier_interval = clock_t_to_jiffies(val);
1053 }
1054
1055 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1056 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1057
1058 br->multicast_query_interval = clock_t_to_jiffies(val);
1059 }
1060
1061 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1062 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1063
1064 br->multicast_query_response_interval = clock_t_to_jiffies(val);
1065 }
1066
1067 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1068 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1069
1070 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
1071 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001072
1073 if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1074 __u8 mcast_stats;
1075
1076 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1077 br->multicast_stats_enabled = !!mcast_stats;
1078 }
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001079
1080 if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1081 __u8 igmp_version;
1082
1083 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1084 err = br_multicast_set_igmp_version(br, igmp_version);
1085 if (err)
1086 return err;
1087 }
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001088
1089#if IS_ENABLED(CONFIG_IPV6)
1090 if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1091 __u8 mld_version;
1092
1093 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1094 err = br_multicast_set_mld_version(br, mld_version);
1095 if (err)
1096 return err;
1097 }
1098#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001099#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001100#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1101 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1102 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1103
1104 br->nf_call_iptables = val ? true : false;
1105 }
1106
1107 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1108 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1109
1110 br->nf_call_ip6tables = val ? true : false;
1111 }
1112
1113 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1114 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1115
1116 br->nf_call_arptables = val ? true : false;
1117 }
1118#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001119
Jiri Pirko13323512014-09-05 15:51:32 +02001120 return 0;
1121}
1122
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001123static size_t br_get_size(const struct net_device *brdev)
1124{
1125 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1126 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1127 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001128 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1129 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1130 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001131 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001132#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1133 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001134 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001135 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001136#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001137 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001138 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001139 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001140 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001141 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001142 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1143 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001144 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1145 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1146 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1147 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001148 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001149#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1150 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001151 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001152 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001153 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001154 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001155 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1156 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1157 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1158 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001159 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1160 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1161 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1162 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1163 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1164 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001165 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001166 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001167#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001168#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1169 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1170 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1171 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1172#endif
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001173 0;
1174}
1175
1176static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1177{
1178 struct net_bridge *br = netdev_priv(brdev);
1179 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1180 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1181 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001182 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1183 u32 stp_enabled = br->stp_enabled;
1184 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001185 u8 vlan_enabled = br_vlan_enabled(br);
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001186 u64 clockval;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001187
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001188 clockval = br_timer_value(&br->hello_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001189 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001190 return -EMSGSIZE;
1191 clockval = br_timer_value(&br->tcn_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001192 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001193 return -EMSGSIZE;
1194 clockval = br_timer_value(&br->topology_change_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001195 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1196 IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001197 return -EMSGSIZE;
1198 clockval = br_timer_value(&br->gc_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001199 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001200 return -EMSGSIZE;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001201
1202 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1203 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001204 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1205 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1206 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001207 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001208 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001209 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1210 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1211 &br->bridge_id) ||
1212 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1213 &br->designated_root) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001214 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001215 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1216 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1217 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001218 br->topology_change_detected) ||
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001219 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001220 return -EMSGSIZE;
1221
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001222#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001223 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001224 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1225 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED, br->vlan_stats_enabled))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001226 return -EMSGSIZE;
1227#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001228#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001229 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001230 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1231 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001232 br->multicast_query_use_ifaddr) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001233 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001234 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1235 br->multicast_stats_enabled) ||
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001236 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001237 br->hash_elasticity) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001238 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1239 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001240 br->multicast_last_member_count) ||
1241 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001242 br->multicast_startup_query_count) ||
1243 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1244 br->multicast_igmp_version))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001245 return -EMSGSIZE;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001246#if IS_ENABLED(CONFIG_IPV6)
1247 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1248 br->multicast_mld_version))
1249 return -EMSGSIZE;
1250#endif
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001251 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001252 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1253 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001254 return -EMSGSIZE;
1255 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001256 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1257 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001258 return -EMSGSIZE;
1259 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001260 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1261 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001262 return -EMSGSIZE;
1263 clockval = jiffies_to_clock_t(br->multicast_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001264 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1265 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001266 return -EMSGSIZE;
1267 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001268 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1269 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001270 return -EMSGSIZE;
1271 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001272 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1273 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001274 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001275#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001276#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1277 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1278 br->nf_call_iptables ? 1 : 0) ||
1279 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1280 br->nf_call_ip6tables ? 1 : 0) ||
1281 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1282 br->nf_call_arptables ? 1 : 0))
1283 return -EMSGSIZE;
1284#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001285
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001286 return 0;
1287}
1288
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001289static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001290{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001291 struct net_bridge_port *p = NULL;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001292 struct net_bridge_vlan_group *vg;
1293 struct net_bridge_vlan *v;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001294 struct net_bridge *br;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001295 int numvls = 0;
1296
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001297 switch (attr) {
1298 case IFLA_STATS_LINK_XSTATS:
1299 br = netdev_priv(dev);
1300 vg = br_vlan_group(br);
1301 break;
1302 case IFLA_STATS_LINK_XSTATS_SLAVE:
1303 p = br_port_get_rtnl(dev);
1304 if (!p)
1305 return 0;
1306 br = p->br;
1307 vg = nbp_vlan_group(p);
1308 break;
1309 default:
1310 return 0;
1311 }
1312
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001313 if (vg) {
1314 /* we need to count all, even placeholder entries */
1315 list_for_each_entry(v, &vg->vlan_list, vlist)
1316 numvls++;
1317 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001318
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001319 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001320 nla_total_size(sizeof(struct br_mcast_stats)) +
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001321 nla_total_size(0);
1322}
1323
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001324static int br_fill_linkxstats(struct sk_buff *skb,
1325 const struct net_device *dev,
1326 int *prividx, int attr)
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001327{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001328 struct nlattr *nla __maybe_unused;
1329 struct net_bridge_port *p = NULL;
1330 struct net_bridge_vlan_group *vg;
1331 struct net_bridge_vlan *v;
1332 struct net_bridge *br;
1333 struct nlattr *nest;
1334 int vl_idx = 0;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001335
1336 switch (attr) {
1337 case IFLA_STATS_LINK_XSTATS:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001338 br = netdev_priv(dev);
1339 vg = br_vlan_group(br);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001340 break;
1341 case IFLA_STATS_LINK_XSTATS_SLAVE:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001342 p = br_port_get_rtnl(dev);
1343 if (!p)
1344 return 0;
1345 br = p->br;
1346 vg = nbp_vlan_group(p);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001347 break;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001348 default:
1349 return -EINVAL;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001350 }
1351
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001352 nest = nla_nest_start(skb, LINK_XSTATS_TYPE_BRIDGE);
1353 if (!nest)
1354 return -EMSGSIZE;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001355
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001356 if (vg) {
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001357 u16 pvid;
1358
1359 pvid = br_get_pvid(vg);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001360 list_for_each_entry(v, &vg->vlan_list, vlist) {
1361 struct bridge_vlan_xstats vxi;
1362 struct br_vlan_stats stats;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001363
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001364 if (++vl_idx < *prividx)
1365 continue;
1366 memset(&vxi, 0, sizeof(vxi));
1367 vxi.vid = v->vid;
Nikolay Aleksandrov61ba1a22016-08-17 12:53:10 +02001368 vxi.flags = v->flags;
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001369 if (v->vid == pvid)
1370 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001371 br_vlan_get_stats(v, &stats);
1372 vxi.rx_bytes = stats.rx_bytes;
1373 vxi.rx_packets = stats.rx_packets;
1374 vxi.tx_bytes = stats.tx_bytes;
1375 vxi.tx_packets = stats.tx_packets;
1376
1377 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1378 goto nla_put_failure;
1379 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001380 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001381
1382#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1383 if (++vl_idx >= *prividx) {
1384 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1385 sizeof(struct br_mcast_stats),
1386 BRIDGE_XSTATS_PAD);
1387 if (!nla)
1388 goto nla_put_failure;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001389 br_multicast_get_stats(br, p, nla_data(nla));
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001390 }
1391#endif
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001392 nla_nest_end(skb, nest);
1393 *prividx = 0;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001394
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001395 return 0;
1396
1397nla_put_failure:
1398 nla_nest_end(skb, nest);
1399 *prividx = vl_idx;
1400
1401 return -EMSGSIZE;
1402}
Roopa Prabhufed0a152015-02-25 23:55:40 -08001403
Daniel Borkmann207895f2015-01-29 12:15:03 +01001404static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001405 .family = AF_BRIDGE,
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001406 .get_link_af_size = br_get_link_af_size_filtered,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001407};
1408
stephen hemminger149ddd82012-06-26 05:48:45 +00001409struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001410 .kind = "bridge",
1411 .priv_size = sizeof(struct net_bridge),
1412 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001413 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001414 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001415 .validate = br_validate,
1416 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001417 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001418 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001419 .get_size = br_get_size,
1420 .fill_info = br_fill_info,
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001421 .fill_linkxstats = br_fill_linkxstats,
1422 .get_linkxstats_size = br_get_linkxstats_size,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001423
1424 .slave_maxtype = IFLA_BRPORT_MAX,
1425 .slave_policy = br_port_policy,
1426 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001427 .get_slave_size = br_port_get_slave_size,
1428 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001429};
1430
1431int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001432{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001433 int err;
1434
1435 br_mdb_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001436 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001437
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001438 err = rtnl_link_register(&br_link_ops);
1439 if (err)
1440 goto out_af;
1441
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001442 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001443
1444out_af:
1445 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001446 br_mdb_uninit();
1447 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001448}
1449
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001450void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001451{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001452 br_mdb_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001453 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001454 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001455}