blob: 92d64abffa87d411699a1cff7fafa95a0bf47d1d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07002/*
3 * Bridge netlink control interface
4 *
5 * Authors:
6 * Stephen Hemminger <shemminger@osdl.org>
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07007 */
8
9#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
stephen hemmingerbb900b22011-04-04 14:03:32 +000011#include <linux/etherdevice.h>
Thomas Graf32fe21c2007-03-22 11:59:03 -070012#include <net/rtnetlink.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070013#include <net/net_namespace.h>
Denis V. Lunevb8542722007-12-01 00:21:31 +110014#include <net/sock.h>
Vlad Yasevich407af322013-02-13 12:00:12 +000015#include <uapi/linux/if_bridge.h>
stephen hemmingerbb900b22011-04-04 14:03:32 +000016
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070017#include "br_private.h"
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +000018#include "br_private_stp.h"
Roopa Prabhuefa53562017-01-31 22:59:54 -080019#include "br_private_tunnel.h"
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070020
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020021static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020022 u32 filter_mask)
Roopa Prabhufed0a152015-02-25 23:55:40 -080023{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020024 struct net_bridge_vlan *v;
25 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020026 u16 flags, pvid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080027 int num_vlans = 0;
28
Roopa Prabhufed0a152015-02-25 23:55:40 -080029 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
30 return 0;
31
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020032 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020033 /* Count number of vlan infos */
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020034 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Roopa Prabhufed0a152015-02-25 23:55:40 -080035 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020036 /* only a context, bridge vlan not activated */
37 if (!br_vlan_should_use(v))
38 continue;
39 if (v->vid == pvid)
Roopa Prabhufed0a152015-02-25 23:55:40 -080040 flags |= BRIDGE_VLAN_INFO_PVID;
41
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020042 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhufed0a152015-02-25 23:55:40 -080043 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
44
45 if (vid_range_start == 0) {
46 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020047 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhufed0a152015-02-25 23:55:40 -080048 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020049 vid_range_end = v->vid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080050 continue;
51 } else {
52 if ((vid_range_end - vid_range_start) > 0)
53 num_vlans += 2;
54 else
55 num_vlans += 1;
56 }
57initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020058 vid_range_start = v->vid;
59 vid_range_end = v->vid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080060 vid_range_flags = flags;
61 }
62
63 if (vid_range_start != 0) {
64 if ((vid_range_end - vid_range_start) > 0)
65 num_vlans += 2;
66 else
67 num_vlans += 1;
68 }
69
70 return num_vlans;
71}
72
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020073static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020074 u32 filter_mask)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020075{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020076 int num_vlans;
77
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020078 if (!vg)
79 return 0;
80
81 if (filter_mask & RTEXT_FILTER_BRVLAN)
82 return vg->num_vlans;
83
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020084 rcu_read_lock();
85 num_vlans = __get_num_vlan_infos(vg, filter_mask);
86 rcu_read_unlock();
87
88 return num_vlans;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020089}
90
Roopa Prabhufed0a152015-02-25 23:55:40 -080091static size_t br_get_link_af_size_filtered(const struct net_device *dev,
92 u32 filter_mask)
Roopa Prabhub7853d72015-02-21 20:21:51 -080093{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020094 struct net_bridge_vlan_group *vg = NULL;
Roopa Prabhuefa53562017-01-31 22:59:54 -080095 struct net_bridge_port *p = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020096 struct net_bridge *br;
Roopa Prabhufed0a152015-02-25 23:55:40 -080097 int num_vlan_infos;
Roopa Prabhuefa53562017-01-31 22:59:54 -080098 size_t vinfo_sz = 0;
Roopa Prabhub7853d72015-02-21 20:21:51 -080099
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100100 rcu_read_lock();
Julian Wiedmann35f861e2019-03-29 14:38:19 +0100101 if (netif_is_bridge_port(dev)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200102 p = br_port_get_rcu(dev);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200103 vg = nbp_vlan_group_rcu(p);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200104 } else if (dev->priv_flags & IFF_EBRIDGE) {
105 br = netdev_priv(dev);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200106 vg = br_vlan_group_rcu(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200107 }
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200108 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100109 rcu_read_unlock();
Roopa Prabhub7853d72015-02-21 20:21:51 -0800110
Roopa Prabhuefa53562017-01-31 22:59:54 -0800111 if (p && (p->flags & BR_VLAN_TUNNEL))
112 vinfo_sz += br_get_vlan_tunnel_info_size(vg);
113
Roopa Prabhub7853d72015-02-21 20:21:51 -0800114 /* Each VLAN is returned in bridge_vlan_info along with flags */
Roopa Prabhuefa53562017-01-31 22:59:54 -0800115 vinfo_sz += num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
116
117 return vinfo_sz;
Roopa Prabhub7853d72015-02-21 20:21:51 -0800118}
119
stephen hemminger25c71c72012-11-13 07:53:05 +0000120static inline size_t br_port_info_size(void)
121{
122 return nla_total_size(1) /* IFLA_BRPORT_STATE */
123 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
124 + nla_total_size(4) /* IFLA_BRPORT_COST */
125 + nla_total_size(1) /* IFLA_BRPORT_MODE */
stephen hemmingera2e01a62012-11-13 07:53:07 +0000126 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
stephen hemminger1007dd12012-11-13 07:53:08 +0000127 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
stephen hemminger3da889b2013-03-11 13:52:17 +0000128 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100129 + nla_total_size(1) /* IFLA_BRPORT_MCAST_TO_UCAST */
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400130 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400131 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
Tobias Klauser90512472017-05-05 16:36:53 +0200132 + nla_total_size(1) /* IFLA_BRPORT_MCAST_FLOOD */
133 + nla_total_size(1) /* IFLA_BRPORT_BCAST_FLOOD */
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200134 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200135 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
Roopa Prabhuefa53562017-01-31 22:59:54 -0800136 + nla_total_size(1) /* IFLA_BRPORT_VLAN_TUNNEL */
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700137 + nla_total_size(1) /* IFLA_BRPORT_NEIGH_SUPPRESS */
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300138 + nla_total_size(1) /* IFLA_BRPORT_ISOLATED */
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200139 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200140 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200141 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
142 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200143 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
144 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200145 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
146 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200147 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
148 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
149 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200150#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
151 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
152#endif
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300153 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_GROUP_FWD_MASK */
Horatiu Vultur3e544422020-04-26 15:22:01 +0200154 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MRP_RING_OPEN */
Horatiu Vulturffb3adb2020-07-14 09:34:58 +0200155 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MRP_IN_OPEN */
stephen hemminger25c71c72012-11-13 07:53:05 +0000156 + 0;
157}
158
Roopa Prabhufed0a152015-02-25 23:55:40 -0800159static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800160{
161 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000162 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
163 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
164 + nla_total_size(4) /* IFLA_MASTER */
165 + nla_total_size(4) /* IFLA_MTU */
166 + nla_total_size(4) /* IFLA_LINK */
167 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800168 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800169 + nla_total_size(br_get_link_af_size_filtered(dev,
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300170 filter_mask)) /* IFLA_AF_SPEC */
171 + nla_total_size(4); /* IFLA_BRPORT_BACKUP_PORT */
stephen hemminger25c71c72012-11-13 07:53:05 +0000172}
173
174static int br_port_fill_attrs(struct sk_buff *skb,
175 const struct net_bridge_port *p)
176{
177 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300178 struct net_bridge_port *backup_p;
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200179 u64 timerval;
stephen hemminger25c71c72012-11-13 07:53:05 +0000180
181 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
182 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
183 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000184 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000185 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200186 nla_put_u8(skb, IFLA_BRPORT_PROTECT,
187 !!(p->flags & BR_ROOT_BLOCK)) ||
188 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
189 !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100190 nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
191 !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400192 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200193 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
194 !!(p->flags & BR_FLOOD)) ||
195 nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
196 !!(p->flags & BR_MCAST_FLOOD)) ||
Mike Manning99f906e2017-04-26 14:48:09 +0100197 nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
198 !!(p->flags & BR_BCAST_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200199 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
200 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200201 !!(p->flags & BR_PROXYARP_WIFI)) ||
202 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200203 &p->designated_root) ||
204 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200205 &p->designated_bridge) ||
206 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200207 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
208 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200209 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
210 nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
211 p->topology_change_ack) ||
Roopa Prabhuefa53562017-01-31 22:59:54 -0800212 nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
213 nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300214 BR_VLAN_TUNNEL)) ||
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700215 nla_put_u16(skb, IFLA_BRPORT_GROUP_FWD_MASK, p->group_fwd_mask) ||
216 nla_put_u8(skb, IFLA_BRPORT_NEIGH_SUPPRESS,
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300217 !!(p->flags & BR_NEIGH_SUPPRESS)) ||
Horatiu Vultur3e544422020-04-26 15:22:01 +0200218 nla_put_u8(skb, IFLA_BRPORT_MRP_RING_OPEN, !!(p->flags &
219 BR_MRP_LOST_CONT)) ||
Horatiu Vulturffb3adb2020-07-14 09:34:58 +0200220 nla_put_u8(skb, IFLA_BRPORT_MRP_IN_OPEN,
221 !!(p->flags & BR_MRP_LOST_IN_CONT)) ||
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300222 nla_put_u8(skb, IFLA_BRPORT_ISOLATED, !!(p->flags & BR_ISOLATED)))
stephen hemminger25c71c72012-11-13 07:53:05 +0000223 return -EMSGSIZE;
224
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200225 timerval = br_timer_value(&p->message_age_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200226 if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
227 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200228 return -EMSGSIZE;
229 timerval = br_timer_value(&p->forward_delay_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200230 if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
231 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200232 return -EMSGSIZE;
233 timerval = br_timer_value(&p->hold_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200234 if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
235 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200236 return -EMSGSIZE;
237
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200238#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
239 if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
240 p->multicast_router))
241 return -EMSGSIZE;
242#endif
243
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300244 /* we might be called only with br->lock */
245 rcu_read_lock();
246 backup_p = rcu_dereference(p->backup_port);
247 if (backup_p)
248 nla_put_u32(skb, IFLA_BRPORT_BACKUP_PORT,
249 backup_p->dev->ifindex);
250 rcu_read_unlock();
251
stephen hemminger25c71c72012-11-13 07:53:05 +0000252 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800253}
254
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800255static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
256 u16 vid_end, u16 flags)
257{
258 struct bridge_vlan_info vinfo;
259
260 if ((vid_end - vid_start) > 0) {
261 /* add range to skb */
262 vinfo.vid = vid_start;
263 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
264 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
265 sizeof(vinfo), &vinfo))
266 goto nla_put_failure;
267
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800268 vinfo.vid = vid_end;
269 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
270 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
271 sizeof(vinfo), &vinfo))
272 goto nla_put_failure;
273 } else {
274 vinfo.vid = vid_start;
275 vinfo.flags = flags;
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
287static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200288 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800289{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200290 struct net_bridge_vlan *v;
291 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200292 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800293 int err = 0;
294
295 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
296 * and mark vlan info with begin and end flags
297 * if vlaninfo represents a range
298 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200299 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200300 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800301 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200302 if (!br_vlan_should_use(v))
303 continue;
304 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800305 flags |= BRIDGE_VLAN_INFO_PVID;
306
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200307 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800308 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
309
310 if (vid_range_start == 0) {
311 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200312 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800313 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200314 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800315 continue;
316 } else {
317 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
318 vid_range_end,
319 vid_range_flags);
320 if (err)
321 return err;
322 }
323
324initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200325 vid_range_start = v->vid;
326 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800327 vid_range_flags = flags;
328 }
329
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800330 if (vid_range_start != 0) {
331 /* Call it once more to send any left over vlans */
332 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
333 vid_range_end,
334 vid_range_flags);
335 if (err)
336 return err;
337 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800338
339 return 0;
340}
341
342static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200343 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800344{
345 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200346 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200347 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800348
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200349 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200350 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200351 if (!br_vlan_should_use(v))
352 continue;
353
354 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800355 vinfo.flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200356 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800357 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
358
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200359 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800360 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
361
362 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
363 sizeof(vinfo), &vinfo))
364 goto nla_put_failure;
365 }
366
367 return 0;
368
369nla_put_failure:
370 return -EMSGSIZE;
371}
372
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700373/*
374 * Create one netlink message for one interface
375 * Contains port and master info as well as carrier and bridge state.
376 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000377static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200378 const struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000379 u32 pid, u32 seq, int event, unsigned int flags,
380 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700381{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200382 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Henrik Bjoernlundb6c02ef2020-10-07 12:07:00 +0000383 struct nlattr *af = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200384 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800385 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700386 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700387
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000388 if (port)
389 br = port->br;
390 else
391 br = netdev_priv(dev);
392
stephen hemminger28a16c92010-05-10 09:31:09 +0000393 br_debug(br, "br_fill_info event %d port %s master %s\n",
394 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700395
Thomas Graf74685962006-11-20 16:20:22 -0800396 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
397 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800398 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700399
Thomas Graf74685962006-11-20 16:20:22 -0800400 hdr = nlmsg_data(nlh);
401 hdr->ifi_family = AF_BRIDGE;
402 hdr->__ifi_pad = 0;
403 hdr->ifi_type = dev->type;
404 hdr->ifi_index = dev->ifindex;
405 hdr->ifi_flags = dev_get_flags(dev);
406 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700407
David S. Miller2eb812e2012-04-01 20:49:54 -0400408 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
409 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
410 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
411 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
412 (dev->addr_len &&
413 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200414 (dev->ifindex != dev_get_iflink(dev) &&
415 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400416 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000417
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000418 if (event == RTM_NEWLINK && port) {
Michal Kubecekf78c6032019-04-26 11:13:12 +0200419 struct nlattr *nest;
stephen hemminger25c71c72012-11-13 07:53:05 +0000420
Michal Kubecekf78c6032019-04-26 11:13:12 +0200421 nest = nla_nest_start(skb, IFLA_PROTINFO);
stephen hemminger25c71c72012-11-13 07:53:05 +0000422 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
423 goto nla_put_failure;
424 nla_nest_end(skb, nest);
425 }
426
Henrik Bjoernlundb6c02ef2020-10-07 12:07:00 +0000427 if (filter_mask & (RTEXT_FILTER_BRVLAN |
428 RTEXT_FILTER_BRVLAN_COMPRESSED |
429 RTEXT_FILTER_MRP)) {
430 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
431 if (!af)
432 goto nla_put_failure;
433 }
434
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000435 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800436 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
437 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200438 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800439 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000440
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200441 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
442 rcu_read_lock();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200443 if (port)
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200444 vg = nbp_vlan_group_rcu(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200445 else
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200446 vg = br_vlan_group_rcu(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000447
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200448 if (!vg || !vg->num_vlans) {
449 rcu_read_unlock();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000450 goto done;
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200451 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800452 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200453 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800454 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200455 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800456
457 if (port && (port->flags & BR_VLAN_TUNNEL))
458 err = br_fill_vlan_tunnel_info(skb, vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200459 rcu_read_unlock();
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800460 if (err)
461 goto nla_put_failure;
Horatiu Vultur36a8e8e2020-07-02 10:13:07 +0200462 }
463
464 if (filter_mask & RTEXT_FILTER_MRP) {
Horatiu Vultur36a8e8e2020-07-02 10:13:07 +0200465 int err;
466
467 if (!br_mrp_enabled(br) || port)
468 goto done;
469
Horatiu Vultur36a8e8e2020-07-02 10:13:07 +0200470 rcu_read_lock();
471 err = br_mrp_fill_info(skb, br);
472 rcu_read_unlock();
473
474 if (err)
475 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000476 }
477
478done:
Henrik Bjoernlundb6c02ef2020-10-07 12:07:00 +0000479 if (af)
480 nla_nest_end(skb, af);
Johannes Berg053c0952015-01-16 22:09:00 +0100481 nlmsg_end(skb, nlh);
482 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700483
Thomas Graf74685962006-11-20 16:20:22 -0800484nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800485 nlmsg_cancel(skb, nlh);
486 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700487}
488
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200489/* Notify listeners of a change in bridge or port information */
490void br_ifinfo_notify(int event, const struct net_bridge *br,
491 const struct net_bridge_port *port)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700492{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200493 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
494 struct net_device *dev;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700495 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700496 int err = -ENOBUFS;
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200497 struct net *net;
498 u16 port_no = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700499
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200500 if (WARN_ON(!port && !br))
Vlad Yasevich407af322013-02-13 12:00:12 +0000501 return;
502
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200503 if (port) {
504 dev = port->dev;
505 br = port->br;
506 port_no = port->port_no;
507 } else {
508 dev = br->dev;
509 }
stephen hemminger28a16c92010-05-10 09:31:09 +0000510
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200511 net = dev_net(dev);
512 br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event);
513
514 skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700515 if (skb == NULL)
516 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700517
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200518 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800519 if (err < 0) {
520 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
521 WARN_ON(err == -EMSGSIZE);
522 kfree_skb(skb);
523 goto errout;
524 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800525 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
526 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700527errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800528 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700529}
530
531/*
532 * Dump information about all ports, in response to GETLINK
533 */
John Fastabende5a55a82012-10-24 08:12:57 +0000534int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200535 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700536{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800537 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700538
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800539 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
Horatiu Vultur36a8e8e2020-07-02 10:13:07 +0200540 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) &&
541 !(filter_mask & RTEXT_FILTER_MRP))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300542 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000543
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200544 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300545 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700546}
547
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800548static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata169327d2018-12-12 17:02:50 +0000549 int cmd, struct bridge_vlan_info *vinfo, bool *changed,
550 struct netlink_ext_ack *extack)
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800551{
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300552 bool curr_change;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800553 int err = 0;
554
555 switch (cmd) {
556 case RTM_SETLINK:
557 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200558 /* if the MASTER flag is set this will act on the global
559 * per-VLAN entry as well
560 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300561 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
Petr Machata169327d2018-12-12 17:02:50 +0000562 &curr_change, extack);
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800563 } else {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200564 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300565 err = br_vlan_add(br, vinfo->vid, vinfo->flags,
Petr Machata169327d2018-12-12 17:02:50 +0000566 &curr_change, extack);
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800567 }
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300568 if (curr_change)
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300569 *changed = true;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800570 break;
571
572 case RTM_DELLINK:
573 if (p) {
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300574 if (!nbp_vlan_delete(p, vinfo->vid))
575 *changed = true;
576
577 if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
578 !br_vlan_delete(p->br, vinfo->vid))
579 *changed = true;
580 } else if (!br_vlan_delete(br, vinfo->vid)) {
581 *changed = true;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800582 }
583 break;
584 }
585
586 return err;
587}
Vlad Yasevich407af322013-02-13 12:00:12 +0000588
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +0200589int br_process_vlan_info(struct net_bridge *br,
590 struct net_bridge_port *p, int cmd,
591 struct bridge_vlan_info *vinfo_curr,
592 struct bridge_vlan_info **vinfo_last,
593 bool *changed,
594 struct netlink_ext_ack *extack)
Roopa Prabhuefa53562017-01-31 22:59:54 -0800595{
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200596 int err, rtm_cmd;
597
Nikolay Aleksandrov8f4cc942020-01-14 19:56:08 +0200598 if (!br_vlan_valid_id(vinfo_curr->vid, extack))
Roopa Prabhuefa53562017-01-31 22:59:54 -0800599 return -EINVAL;
600
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200601 /* needed for vlan-only NEWVLAN/DELVLAN notifications */
602 rtm_cmd = br_afspec_cmd_to_rtm(cmd);
603
Roopa Prabhuefa53562017-01-31 22:59:54 -0800604 if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Nikolay Aleksandrov8f4cc942020-01-14 19:56:08 +0200605 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
Roopa Prabhuefa53562017-01-31 22:59:54 -0800606 return -EINVAL;
607 *vinfo_last = vinfo_curr;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800608 return 0;
609 }
610
611 if (*vinfo_last) {
612 struct bridge_vlan_info tmp_vinfo;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200613 int v, v_change_start = 0;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800614
Nikolay Aleksandrov8f4cc942020-01-14 19:56:08 +0200615 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
Roopa Prabhuefa53562017-01-31 22:59:54 -0800616 return -EINVAL;
617
618 memcpy(&tmp_vinfo, *vinfo_last,
619 sizeof(struct bridge_vlan_info));
620 for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200621 bool curr_change = false;
622
Roopa Prabhuefa53562017-01-31 22:59:54 -0800623 tmp_vinfo.vid = v;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200624 err = br_vlan_info(br, p, cmd, &tmp_vinfo, &curr_change,
Petr Machata169327d2018-12-12 17:02:50 +0000625 extack);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800626 if (err)
627 break;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200628 if (curr_change) {
629 *changed = curr_change;
630 if (!v_change_start)
631 v_change_start = v;
632 } else {
633 /* nothing to notify yet */
634 if (!v_change_start)
635 continue;
636 br_vlan_notify(br, p, v_change_start,
637 v - 1, rtm_cmd);
638 v_change_start = 0;
639 }
Ido Schimmel79794572020-04-30 22:38:45 +0300640 cond_resched();
Roopa Prabhuefa53562017-01-31 22:59:54 -0800641 }
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200642 /* v_change_start is set only if the last/whole range changed */
643 if (v_change_start)
644 br_vlan_notify(br, p, v_change_start,
645 v - 1, rtm_cmd);
646
Roopa Prabhuefa53562017-01-31 22:59:54 -0800647 *vinfo_last = NULL;
648
Nikolay Aleksandrov66c54512017-10-19 20:17:32 +0300649 return err;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800650 }
651
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200652 err = br_vlan_info(br, p, cmd, vinfo_curr, changed, extack);
653 if (*changed)
654 br_vlan_notify(br, p, vinfo_curr->vid, 0, rtm_cmd);
655
656 return err;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800657}
658
Vlad Yasevich407af322013-02-13 12:00:12 +0000659static int br_afspec(struct net_bridge *br,
660 struct net_bridge_port *p,
661 struct nlattr *af_spec,
Petr Machata169327d2018-12-12 17:02:50 +0000662 int cmd, bool *changed,
663 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +0000664{
Roopa Prabhuefa53562017-01-31 22:59:54 -0800665 struct bridge_vlan_info *vinfo_curr = NULL;
666 struct bridge_vlan_info *vinfo_last = NULL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800667 struct nlattr *attr;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800668 struct vtunnel_info tinfo_last = {};
669 struct vtunnel_info tinfo_curr = {};
670 int err = 0, rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000671
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800672 nla_for_each_nested(attr, af_spec, rem) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800673 err = 0;
674 switch (nla_type(attr)) {
675 case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
Nikolay Aleksandrov1020ce32017-06-06 01:26:24 +0300676 if (!p || !(p->flags & BR_VLAN_TUNNEL))
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800677 return -EINVAL;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800678 err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
679 if (err)
680 return err;
681 err = br_process_vlan_tunnel_info(br, p, cmd,
682 &tinfo_curr,
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300683 &tinfo_last,
684 changed);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800685 if (err)
686 return err;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800687 break;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800688 case IFLA_BRIDGE_VLAN_INFO:
689 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
690 return -EINVAL;
691 vinfo_curr = nla_data(attr);
692 err = br_process_vlan_info(br, p, cmd, vinfo_curr,
Petr Machata169327d2018-12-12 17:02:50 +0000693 &vinfo_last, changed,
694 extack);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800695 if (err)
696 return err;
697 break;
Horatiu Vultur65369932020-04-26 15:22:07 +0200698 case IFLA_BRIDGE_MRP:
699 err = br_mrp_parse(br, p, attr, cmd, extack);
700 if (err)
701 return err;
702 break;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800703 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000704 }
705
706 return err;
707}
708
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200709static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000710 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
711 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
712 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
713 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000714 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000715 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100716 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400717 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400718 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200719 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200720 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200721 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100722 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
Tobias Klauser90512472017-05-05 16:36:53 +0200723 [IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
724 [IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrovfbec4432017-11-13 18:30:55 +0200725 [IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300726 [IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov1a3fbd32017-10-30 12:56:33 +0200727 [IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300728 [IFLA_BRPORT_ISOLATED] = { .type = NLA_U8 },
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300729 [IFLA_BRPORT_BACKUP_PORT] = { .type = NLA_U32 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000730};
731
732/* Change the state of the port and notify spanning tree */
733static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700734{
stephen hemminger25c71c72012-11-13 07:53:05 +0000735 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000736 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700737
738 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700739 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700740 return -EBUSY;
741
stephen hemminger576eb622012-12-28 18:15:22 +0000742 /* if device is not up, change is not allowed
743 * if link is not present, only allowable state is disabled
744 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000745 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000746 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700747 return -ENETDOWN;
748
Florian Fainelli775dd692014-09-30 16:13:19 -0700749 br_set_state(p, state);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000750 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700751 return 0;
752}
753
stephen hemminger25c71c72012-11-13 07:53:05 +0000754/* Set/clear or port flags based on attribute */
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200755static int br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
756 int attrtype, unsigned long mask)
stephen hemminger25c71c72012-11-13 07:53:05 +0000757{
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200758 unsigned long flags;
759 int err;
760
761 if (!tb[attrtype])
762 return 0;
763
764 if (nla_get_u8(tb[attrtype]))
765 flags = p->flags | mask;
766 else
767 flags = p->flags & ~mask;
768
769 err = br_switchdev_set_port_flag(p, flags, mask);
770 if (err)
771 return err;
772
773 p->flags = flags;
774 return 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000775}
776
777/* Process bridge protocol info on port */
778static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
779{
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400780 unsigned long old_flags = p->flags;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800781 bool br_vlan_tunnel_old = false;
782 int err;
stephen hemminger25c71c72012-11-13 07:53:05 +0000783
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200784 err = br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
785 if (err)
786 return err;
787
788 err = br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
789 if (err)
790 return err;
791
792 err = br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
793 if (err)
794 return err;
795
796 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
797 if (err)
798 return err;
799
800 err = br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
801 if (err)
802 return err;
803
804 err = br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
805 if (err)
806 return err;
807
808 err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
809 if (err)
810 return err;
811
812 err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
813 if (err)
814 return err;
815
816 err = br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
817 if (err)
818 return err;
819
820 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
821 if (err)
822 return err;
823
824 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
825 if (err)
826 return err;
stephen hemminger25c71c72012-11-13 07:53:05 +0000827
Roopa Prabhuefa53562017-01-31 22:59:54 -0800828 br_vlan_tunnel_old = (p->flags & BR_VLAN_TUNNEL) ? true : false;
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200829 err = br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
830 if (err)
831 return err;
832
Roopa Prabhuefa53562017-01-31 22:59:54 -0800833 if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
834 nbp_vlan_tunnel_info_flush(p);
835
stephen hemminger25c71c72012-11-13 07:53:05 +0000836 if (tb[IFLA_BRPORT_COST]) {
837 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
838 if (err)
839 return err;
840 }
841
842 if (tb[IFLA_BRPORT_PRIORITY]) {
843 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
844 if (err)
845 return err;
846 }
847
848 if (tb[IFLA_BRPORT_STATE]) {
849 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
850 if (err)
851 return err;
852 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400853
Nikolay Aleksandrov9b0c6e42015-10-06 14:12:01 +0200854 if (tb[IFLA_BRPORT_FLUSH])
855 br_fdb_delete_by_port(p->br, p, 0, 0);
856
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200857#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
858 if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
859 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
860
861 err = br_multicast_set_port_router(p, mcast_router);
862 if (err)
863 return err;
864 }
865#endif
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300866
867 if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
868 u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
869
870 if (fwd_mask & BR_GROUPFWD_MACPAUSE)
871 return -EINVAL;
872 p->group_fwd_mask = fwd_mask;
873 }
874
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700875 err = br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS,
876 BR_NEIGH_SUPPRESS);
877 if (err)
878 return err;
879
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300880 err = br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
881 if (err)
882 return err;
883
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300884 if (tb[IFLA_BRPORT_BACKUP_PORT]) {
885 struct net_device *backup_dev = NULL;
886 u32 backup_ifindex;
887
888 backup_ifindex = nla_get_u32(tb[IFLA_BRPORT_BACKUP_PORT]);
889 if (backup_ifindex) {
890 backup_dev = __dev_get_by_index(dev_net(p->dev),
891 backup_ifindex);
892 if (!backup_dev)
893 return -ENOENT;
894 }
895
896 err = nbp_backup_change(p, backup_dev);
897 if (err)
898 return err;
899 }
900
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400901 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000902 return 0;
903}
904
905/* Change state and parameters on port. */
Petr Machata2fd527b2018-12-12 17:02:48 +0000906int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags,
907 struct netlink_ext_ack *extack)
stephen hemminger25c71c72012-11-13 07:53:05 +0000908{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200909 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
910 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
911 struct net_bridge_port *p;
stephen hemminger25c71c72012-11-13 07:53:05 +0000912 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000913 struct nlattr *afspec;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300914 bool changed = false;
Scott Feldman41c498b2015-05-10 09:47:59 -0700915 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000916
Hong zhi guoc60ee672013-03-28 06:21:22 +0000917 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
918 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000919 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000920 return 0;
921
922 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000923 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800924 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000925 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000926 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000927 return -EINVAL;
928
Vlad Yasevich407af322013-02-13 12:00:12 +0000929 if (p && protinfo) {
930 if (protinfo->nla_type & NLA_F_NESTED) {
Johannes Berg8cb08172019-04-26 14:07:28 +0200931 err = nla_parse_nested_deprecated(tb, IFLA_BRPORT_MAX,
932 protinfo,
933 br_port_policy,
934 NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +0000935 if (err)
936 return err;
937
938 spin_lock_bh(&p->br->lock);
939 err = br_setport(p, tb);
940 spin_unlock_bh(&p->br->lock);
941 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800942 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000943 if (nla_len(protinfo) < sizeof(u8))
944 return -EINVAL;
945
946 spin_lock_bh(&p->br->lock);
947 err = br_set_port_state(p, nla_get_u8(protinfo));
948 spin_unlock_bh(&p->br->lock);
949 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000950 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000951 goto out;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300952 changed = true;
Vlad Yasevich407af322013-02-13 12:00:12 +0000953 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000954
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200955 if (afspec)
Petr Machata169327d2018-12-12 17:02:50 +0000956 err = br_afspec(br, p, afspec, RTM_SETLINK, &changed, extack);
stephen hemminger25c71c72012-11-13 07:53:05 +0000957
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300958 if (changed)
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200959 br_ifinfo_notify(RTM_NEWLINK, br, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000960out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000961 return err;
962}
963
Vlad Yasevich407af322013-02-13 12:00:12 +0000964/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800965int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000966{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200967 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000968 struct net_bridge_port *p;
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200969 struct nlattr *afspec;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300970 bool changed = false;
Scott Feldman85080252015-05-10 09:48:03 -0700971 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000972
Hong zhi guoc60ee672013-03-28 06:21:22 +0000973 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000974 if (!afspec)
975 return 0;
976
977 p = br_port_get_rtnl(dev);
978 /* We want to accept dev as bridge itself as well */
979 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
980 return -EINVAL;
981
Petr Machata169327d2018-12-12 17:02:50 +0000982 err = br_afspec(br, p, afspec, RTM_DELLINK, &changed, NULL);
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300983 if (changed)
Roopa Prabhu02dba432015-01-14 20:02:25 -0800984 /* Send RTM_NEWLINK because userspace
985 * expects RTM_NEWLINK for vlan dels
986 */
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200987 br_ifinfo_notify(RTM_NEWLINK, br, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000988
989 return err;
990}
Matthias Schiffera8b8a8892017-06-25 23:56:01 +0200991
992static int br_validate(struct nlattr *tb[], struct nlattr *data[],
993 struct netlink_ext_ack *extack)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700994{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000995 if (tb[IFLA_ADDRESS]) {
996 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
997 return -EINVAL;
998 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
999 return -EADDRNOTAVAIL;
1000 }
Thomas Graf32fe21c2007-03-22 11:59:03 -07001001
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001002 if (!data)
1003 return 0;
1004
1005#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1006 if (data[IFLA_BR_VLAN_PROTOCOL]) {
1007 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
1008 case htons(ETH_P_8021Q):
1009 case htons(ETH_P_8021AD):
1010 break;
1011 default:
1012 return -EPROTONOSUPPORT;
1013 }
1014 }
Tobias Jungela2858602017-05-17 09:29:12 +02001015
1016 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1017 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1018
1019 if (defpvid >= VLAN_VID_MASK)
1020 return -EINVAL;
1021 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001022#endif
1023
Thomas Graf32fe21c2007-03-22 11:59:03 -07001024 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001025}
1026
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001027static int br_port_slave_changelink(struct net_device *brdev,
1028 struct net_device *dev,
1029 struct nlattr *tb[],
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02001030 struct nlattr *data[],
1031 struct netlink_ext_ack *extack)
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001032{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +02001033 struct net_bridge *br = netdev_priv(brdev);
1034 int ret;
1035
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001036 if (!data)
1037 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +02001038
1039 spin_lock_bh(&br->lock);
1040 ret = br_setport(br_port_get_rtnl(dev), data);
1041 spin_unlock_bh(&br->lock);
1042
1043 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001044}
1045
Jiri Pirkoced82832014-09-05 15:51:29 +02001046static int br_port_fill_slave_info(struct sk_buff *skb,
1047 const struct net_device *brdev,
1048 const struct net_device *dev)
1049{
1050 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
1051}
1052
1053static size_t br_port_get_slave_size(const struct net_device *brdev,
1054 const struct net_device *dev)
1055{
1056 return br_port_info_size();
1057}
1058
Jiri Pirko13323512014-09-05 15:51:32 +02001059static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
1060 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
1061 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
1062 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +01001063 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
1064 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
1065 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001066 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001067 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001068 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001069 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
1070 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001071 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001072 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001073 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001074 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001075 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001076 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001077 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001078 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001079 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1080 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1081 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1082 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1083 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1084 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001085 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1086 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1087 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001088 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001089 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001090 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001091 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001092 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001093 [IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
Johannes Berg81408602020-08-18 10:17:31 +02001094 [IFLA_BR_MULTI_BOOLOPT] =
1095 NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)),
Jiri Pirko13323512014-09-05 15:51:32 +02001096};
1097
1098static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001099 struct nlattr *data[],
1100 struct netlink_ext_ack *extack)
Jiri Pirko13323512014-09-05 15:51:32 +02001101{
1102 struct net_bridge *br = netdev_priv(brdev);
1103 int err;
1104
1105 if (!data)
1106 return 0;
1107
1108 if (data[IFLA_BR_FORWARD_DELAY]) {
1109 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1110 if (err)
1111 return err;
1112 }
1113
1114 if (data[IFLA_BR_HELLO_TIME]) {
1115 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1116 if (err)
1117 return err;
1118 }
1119
1120 if (data[IFLA_BR_MAX_AGE]) {
1121 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1122 if (err)
1123 return err;
1124 }
1125
Jörg Thalheimaf615762015-03-18 10:06:58 +01001126 if (data[IFLA_BR_AGEING_TIME]) {
Scott Feldmanc62987b2015-10-08 19:23:19 -07001127 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1128 if (err)
1129 return err;
Jörg Thalheimaf615762015-03-18 10:06:58 +01001130 }
1131
1132 if (data[IFLA_BR_STP_STATE]) {
1133 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1134
Horatiu Vultur419dba8a2020-04-26 15:22:08 +02001135 err = br_stp_set_enabled(br, stp_enabled, extack);
1136 if (err)
1137 return err;
Jörg Thalheimaf615762015-03-18 10:06:58 +01001138 }
1139
1140 if (data[IFLA_BR_PRIORITY]) {
1141 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1142
1143 br_stp_set_bridge_priority(br, priority);
1144 }
1145
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001146 if (data[IFLA_BR_VLAN_FILTERING]) {
1147 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1148
1149 err = __br_vlan_filter_toggle(br, vlan_filter);
1150 if (err)
1151 return err;
1152 }
1153
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001154#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1155 if (data[IFLA_BR_VLAN_PROTOCOL]) {
1156 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1157
1158 err = __br_vlan_set_proto(br, vlan_proto);
1159 if (err)
1160 return err;
1161 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001162
1163 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1164 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1165
Petr Machata169327d2018-12-12 17:02:50 +00001166 err = __br_vlan_set_default_pvid(br, defpvid, extack);
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001167 if (err)
1168 return err;
1169 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001170
1171 if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1172 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1173
1174 err = br_vlan_set_stats(br, vlan_stats);
1175 if (err)
1176 return err;
1177 }
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001178
1179 if (data[IFLA_BR_VLAN_STATS_PER_PORT]) {
1180 __u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]);
1181
1182 err = br_vlan_set_stats_per_port(br, per_port);
1183 if (err)
1184 return err;
1185 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001186#endif
1187
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001188 if (data[IFLA_BR_GROUP_FWD_MASK]) {
1189 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1190
1191 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1192 return -EINVAL;
1193 br->group_fwd_mask = fwd_mask;
1194 }
1195
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001196 if (data[IFLA_BR_GROUP_ADDR]) {
1197 u8 new_addr[ETH_ALEN];
1198
1199 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1200 return -EINVAL;
1201 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1202 if (!is_link_local_ether_addr(new_addr))
1203 return -EINVAL;
1204 if (new_addr[5] == 1 || /* 802.3x Pause address */
1205 new_addr[5] == 2 || /* 802.3ad Slow protocols */
1206 new_addr[5] == 3) /* 802.1X PAE address */
1207 return -EINVAL;
1208 spin_lock_bh(&br->lock);
1209 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1210 spin_unlock_bh(&br->lock);
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +03001211 br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001212 br_recalculate_fwd_mask(br);
1213 }
1214
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +02001215 if (data[IFLA_BR_FDB_FLUSH])
1216 br_fdb_flush(br);
1217
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001218#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1219 if (data[IFLA_BR_MCAST_ROUTER]) {
1220 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1221
1222 err = br_multicast_set_router(br, multicast_router);
1223 if (err)
1224 return err;
1225 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001226
1227 if (data[IFLA_BR_MCAST_SNOOPING]) {
1228 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1229
YueHaibinga26d94b2018-12-17 17:46:23 +08001230 br_multicast_toggle(br, mcast_snooping);
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001231 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001232
1233 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1234 u8 val;
1235
1236 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001237 br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001238 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001239
1240 if (data[IFLA_BR_MCAST_QUERIER]) {
1241 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1242
1243 err = br_multicast_set_querier(br, mcast_querier);
1244 if (err)
1245 return err;
1246 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001247
Nikolay Aleksandrovcf332bca2018-12-05 15:14:26 +02001248 if (data[IFLA_BR_MCAST_HASH_ELASTICITY])
1249 br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
1250 RHT_ELASTICITY);
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001251
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001252 if (data[IFLA_BR_MCAST_HASH_MAX])
1253 br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001254
1255 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1256 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1257
1258 br->multicast_last_member_count = val;
1259 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001260
1261 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1262 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1263
1264 br->multicast_startup_query_count = val;
1265 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001266
1267 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1268 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1269
1270 br->multicast_last_member_interval = clock_t_to_jiffies(val);
1271 }
1272
1273 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1274 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1275
1276 br->multicast_membership_interval = clock_t_to_jiffies(val);
1277 }
1278
1279 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1280 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1281
1282 br->multicast_querier_interval = clock_t_to_jiffies(val);
1283 }
1284
1285 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1286 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1287
1288 br->multicast_query_interval = clock_t_to_jiffies(val);
1289 }
1290
1291 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1292 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1293
1294 br->multicast_query_response_interval = clock_t_to_jiffies(val);
1295 }
1296
1297 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1298 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1299
1300 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
1301 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001302
1303 if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1304 __u8 mcast_stats;
1305
1306 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001307 br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001308 }
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001309
1310 if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1311 __u8 igmp_version;
1312
1313 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1314 err = br_multicast_set_igmp_version(br, igmp_version);
1315 if (err)
1316 return err;
1317 }
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001318
1319#if IS_ENABLED(CONFIG_IPV6)
1320 if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1321 __u8 mld_version;
1322
1323 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1324 err = br_multicast_set_mld_version(br, mld_version);
1325 if (err)
1326 return err;
1327 }
1328#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001329#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001330#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1331 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1332 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1333
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001334 br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001335 }
1336
1337 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1338 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1339
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001340 br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001341 }
1342
1343 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1344 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1345
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001346 br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001347 }
1348#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001349
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001350 if (data[IFLA_BR_MULTI_BOOLOPT]) {
1351 struct br_boolopt_multi *bm;
1352
1353 bm = nla_data(data[IFLA_BR_MULTI_BOOLOPT]);
1354 err = br_boolopt_multi_toggle(br, bm, extack);
1355 if (err)
1356 return err;
1357 }
1358
Jiri Pirko13323512014-09-05 15:51:32 +02001359 return 0;
1360}
1361
Ivan Vecerab6677442017-01-20 18:12:17 +01001362static int br_dev_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001363 struct nlattr *tb[], struct nlattr *data[],
1364 struct netlink_ext_ack *extack)
Ivan Vecerab6677442017-01-20 18:12:17 +01001365{
1366 struct net_bridge *br = netdev_priv(dev);
1367 int err;
1368
Nikolay Aleksandrov84aeb432017-12-18 17:35:09 +02001369 err = register_netdevice(dev);
1370 if (err)
1371 return err;
1372
Ivan Vecerab6677442017-01-20 18:12:17 +01001373 if (tb[IFLA_ADDRESS]) {
1374 spin_lock_bh(&br->lock);
1375 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1376 spin_unlock_bh(&br->lock);
1377 }
1378
Matthias Schifferad744b22017-06-25 23:56:00 +02001379 err = br_changelink(dev, tb, data, extack);
Ido Schimmel5b8d5422017-04-10 14:59:28 +03001380 if (err)
Nikolay Aleksandrov84aeb432017-12-18 17:35:09 +02001381 br_dev_delete(dev, NULL);
1382
Ido Schimmel5b8d5422017-04-10 14:59:28 +03001383 return err;
Ivan Vecerab6677442017-01-20 18:12:17 +01001384}
1385
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001386static size_t br_get_size(const struct net_device *brdev)
1387{
1388 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1389 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1390 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001391 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1392 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1393 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001394 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001395#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1396 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001397 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001398 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001399 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_PER_PORT */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001400#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001401 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001402 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001403 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001404 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001405 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001406 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1407 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001408 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1409 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1410 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1411 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001412 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001413#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1414 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001415 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001416 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001417 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001418 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001419 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1420 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1421 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1422 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001423 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1424 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1425 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1426 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1427 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1428 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001429 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001430 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001431#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001432#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1433 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1434 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1435 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1436#endif
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001437 nla_total_size(sizeof(struct br_boolopt_multi)) + /* IFLA_BR_MULTI_BOOLOPT */
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001438 0;
1439}
1440
1441static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1442{
1443 struct net_bridge *br = netdev_priv(brdev);
1444 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1445 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1446 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001447 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1448 u32 stp_enabled = br->stp_enabled;
1449 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Ido Schimmel1f514452017-05-26 08:37:23 +02001450 u8 vlan_enabled = br_vlan_enabled(br->dev);
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001451 struct br_boolopt_multi bm;
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001452 u64 clockval;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001453
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001454 clockval = br_timer_value(&br->hello_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001455 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001456 return -EMSGSIZE;
1457 clockval = br_timer_value(&br->tcn_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001458 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001459 return -EMSGSIZE;
1460 clockval = br_timer_value(&br->topology_change_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001461 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1462 IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001463 return -EMSGSIZE;
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +01001464 clockval = br_timer_value(&br->gc_work.timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001465 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001466 return -EMSGSIZE;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001467
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001468 br_boolopt_multi_get(br, &bm);
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001469 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1470 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001471 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1472 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1473 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001474 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001475 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001476 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1477 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1478 &br->bridge_id) ||
1479 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1480 &br->designated_root) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001481 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001482 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1483 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1484 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001485 br->topology_change_detected) ||
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001486 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) ||
1487 nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001488 return -EMSGSIZE;
1489
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001490#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001491 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001492 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001493 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED,
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001494 br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) ||
1495 nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT,
Nikolay Aleksandrov600bea72019-04-16 16:15:56 +03001496 br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001497 return -EMSGSIZE;
1498#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001499#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001500 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03001501 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING,
1502 br_opt_get(br, BROPT_MULTICAST_ENABLED)) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001503 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001504 br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) ||
1505 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER,
1506 br_opt_get(br, BROPT_MULTICAST_QUERIER)) ||
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001507 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001508 br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
Nikolay Aleksandrovcf332bca2018-12-05 15:14:26 +02001509 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001510 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1511 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001512 br->multicast_last_member_count) ||
1513 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001514 br->multicast_startup_query_count) ||
1515 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1516 br->multicast_igmp_version))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001517 return -EMSGSIZE;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001518#if IS_ENABLED(CONFIG_IPV6)
1519 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1520 br->multicast_mld_version))
1521 return -EMSGSIZE;
1522#endif
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001523 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001524 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1525 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001526 return -EMSGSIZE;
1527 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001528 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1529 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001530 return -EMSGSIZE;
1531 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001532 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1533 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001534 return -EMSGSIZE;
1535 clockval = jiffies_to_clock_t(br->multicast_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001536 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1537 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001538 return -EMSGSIZE;
1539 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001540 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1541 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001542 return -EMSGSIZE;
1543 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001544 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1545 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001546 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001547#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001548#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1549 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001550 br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) ||
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001551 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001552 br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) ||
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001553 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001554 br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0))
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001555 return -EMSGSIZE;
1556#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001557
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001558 return 0;
1559}
1560
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001561static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001562{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001563 struct net_bridge_port *p = NULL;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001564 struct net_bridge_vlan_group *vg;
1565 struct net_bridge_vlan *v;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001566 struct net_bridge *br;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001567 int numvls = 0;
1568
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001569 switch (attr) {
1570 case IFLA_STATS_LINK_XSTATS:
1571 br = netdev_priv(dev);
1572 vg = br_vlan_group(br);
1573 break;
1574 case IFLA_STATS_LINK_XSTATS_SLAVE:
1575 p = br_port_get_rtnl(dev);
1576 if (!p)
1577 return 0;
1578 br = p->br;
1579 vg = nbp_vlan_group(p);
1580 break;
1581 default:
1582 return 0;
1583 }
1584
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001585 if (vg) {
1586 /* we need to count all, even placeholder entries */
1587 list_for_each_entry(v, &vg->vlan_list, vlist)
1588 numvls++;
1589 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001590
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001591 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001592 nla_total_size(sizeof(struct br_mcast_stats)) +
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001593 nla_total_size(0);
1594}
1595
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001596static int br_fill_linkxstats(struct sk_buff *skb,
1597 const struct net_device *dev,
1598 int *prividx, int attr)
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001599{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001600 struct nlattr *nla __maybe_unused;
1601 struct net_bridge_port *p = NULL;
1602 struct net_bridge_vlan_group *vg;
1603 struct net_bridge_vlan *v;
1604 struct net_bridge *br;
1605 struct nlattr *nest;
1606 int vl_idx = 0;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001607
1608 switch (attr) {
1609 case IFLA_STATS_LINK_XSTATS:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001610 br = netdev_priv(dev);
1611 vg = br_vlan_group(br);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001612 break;
1613 case IFLA_STATS_LINK_XSTATS_SLAVE:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001614 p = br_port_get_rtnl(dev);
1615 if (!p)
1616 return 0;
1617 br = p->br;
1618 vg = nbp_vlan_group(p);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001619 break;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001620 default:
1621 return -EINVAL;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001622 }
1623
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001624 nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE);
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001625 if (!nest)
1626 return -EMSGSIZE;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001627
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001628 if (vg) {
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001629 u16 pvid;
1630
1631 pvid = br_get_pvid(vg);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001632 list_for_each_entry(v, &vg->vlan_list, vlist) {
1633 struct bridge_vlan_xstats vxi;
1634 struct br_vlan_stats stats;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001635
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001636 if (++vl_idx < *prividx)
1637 continue;
1638 memset(&vxi, 0, sizeof(vxi));
1639 vxi.vid = v->vid;
Nikolay Aleksandrov61ba1a22016-08-17 12:53:10 +02001640 vxi.flags = v->flags;
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001641 if (v->vid == pvid)
1642 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001643 br_vlan_get_stats(v, &stats);
1644 vxi.rx_bytes = stats.rx_bytes;
1645 vxi.rx_packets = stats.rx_packets;
1646 vxi.tx_bytes = stats.tx_bytes;
1647 vxi.tx_packets = stats.tx_packets;
1648
1649 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1650 goto nla_put_failure;
1651 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001652 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001653
1654#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1655 if (++vl_idx >= *prividx) {
1656 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1657 sizeof(struct br_mcast_stats),
1658 BRIDGE_XSTATS_PAD);
1659 if (!nla)
1660 goto nla_put_failure;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001661 br_multicast_get_stats(br, p, nla_data(nla));
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001662 }
1663#endif
Vivien Didelotde179962019-12-11 20:07:10 -05001664
1665 if (p) {
1666 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_STP,
1667 sizeof(p->stp_xstats),
1668 BRIDGE_XSTATS_PAD);
1669 if (!nla)
1670 goto nla_put_failure;
1671
1672 spin_lock_bh(&br->lock);
1673 memcpy(nla_data(nla), &p->stp_xstats, sizeof(p->stp_xstats));
1674 spin_unlock_bh(&br->lock);
1675 }
1676
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001677 nla_nest_end(skb, nest);
1678 *prividx = 0;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001679
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001680 return 0;
1681
1682nla_put_failure:
1683 nla_nest_end(skb, nest);
1684 *prividx = vl_idx;
1685
1686 return -EMSGSIZE;
1687}
Roopa Prabhufed0a152015-02-25 23:55:40 -08001688
Daniel Borkmann207895fd32015-01-29 12:15:03 +01001689static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001690 .family = AF_BRIDGE,
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001691 .get_link_af_size = br_get_link_af_size_filtered,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001692};
1693
stephen hemminger149ddd82012-06-26 05:48:45 +00001694struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001695 .kind = "bridge",
1696 .priv_size = sizeof(struct net_bridge),
1697 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001698 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001699 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001700 .validate = br_validate,
1701 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001702 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001703 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001704 .get_size = br_get_size,
1705 .fill_info = br_fill_info,
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001706 .fill_linkxstats = br_fill_linkxstats,
1707 .get_linkxstats_size = br_get_linkxstats_size,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001708
1709 .slave_maxtype = IFLA_BRPORT_MAX,
1710 .slave_policy = br_port_policy,
1711 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001712 .get_slave_size = br_port_get_slave_size,
1713 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001714};
1715
1716int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001717{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001718 int err;
1719
1720 br_mdb_init();
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001721 br_vlan_rtnl_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001722 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001723
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001724 err = rtnl_link_register(&br_link_ops);
1725 if (err)
1726 goto out_af;
1727
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001728 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001729
1730out_af:
1731 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001732 br_mdb_uninit();
1733 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001734}
1735
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001736void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001737{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001738 br_mdb_uninit();
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001739 br_vlan_rtnl_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001740 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001741 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001742}