blob: 1a5e681a626a0fcb703d0f44be0b4055b725dd7b [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 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000155 + 0;
156}
157
Roopa Prabhufed0a152015-02-25 23:55:40 -0800158static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800159{
160 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000161 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
162 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
163 + nla_total_size(4) /* IFLA_MASTER */
164 + nla_total_size(4) /* IFLA_MTU */
165 + nla_total_size(4) /* IFLA_LINK */
166 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800167 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800168 + nla_total_size(br_get_link_af_size_filtered(dev,
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300169 filter_mask)) /* IFLA_AF_SPEC */
170 + nla_total_size(4); /* IFLA_BRPORT_BACKUP_PORT */
stephen hemminger25c71c72012-11-13 07:53:05 +0000171}
172
173static int br_port_fill_attrs(struct sk_buff *skb,
174 const struct net_bridge_port *p)
175{
176 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300177 struct net_bridge_port *backup_p;
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200178 u64 timerval;
stephen hemminger25c71c72012-11-13 07:53:05 +0000179
180 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
181 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
182 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000183 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000184 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200185 nla_put_u8(skb, IFLA_BRPORT_PROTECT,
186 !!(p->flags & BR_ROOT_BLOCK)) ||
187 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
188 !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100189 nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
190 !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400191 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200192 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
193 !!(p->flags & BR_FLOOD)) ||
194 nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
195 !!(p->flags & BR_MCAST_FLOOD)) ||
Mike Manning99f906e2017-04-26 14:48:09 +0100196 nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
197 !!(p->flags & BR_BCAST_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200198 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
199 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200200 !!(p->flags & BR_PROXYARP_WIFI)) ||
201 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200202 &p->designated_root) ||
203 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200204 &p->designated_bridge) ||
205 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200206 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
207 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200208 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
209 nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
210 p->topology_change_ack) ||
Roopa Prabhuefa53562017-01-31 22:59:54 -0800211 nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
212 nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300213 BR_VLAN_TUNNEL)) ||
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700214 nla_put_u16(skb, IFLA_BRPORT_GROUP_FWD_MASK, p->group_fwd_mask) ||
215 nla_put_u8(skb, IFLA_BRPORT_NEIGH_SUPPRESS,
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300216 !!(p->flags & BR_NEIGH_SUPPRESS)) ||
Horatiu Vultur3e544422020-04-26 15:22:01 +0200217 nla_put_u8(skb, IFLA_BRPORT_MRP_RING_OPEN, !!(p->flags &
218 BR_MRP_LOST_CONT)) ||
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300219 nla_put_u8(skb, IFLA_BRPORT_ISOLATED, !!(p->flags & BR_ISOLATED)))
stephen hemminger25c71c72012-11-13 07:53:05 +0000220 return -EMSGSIZE;
221
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200222 timerval = br_timer_value(&p->message_age_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200223 if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
224 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200225 return -EMSGSIZE;
226 timerval = br_timer_value(&p->forward_delay_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200227 if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
228 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200229 return -EMSGSIZE;
230 timerval = br_timer_value(&p->hold_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200231 if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
232 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200233 return -EMSGSIZE;
234
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200235#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
236 if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
237 p->multicast_router))
238 return -EMSGSIZE;
239#endif
240
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300241 /* we might be called only with br->lock */
242 rcu_read_lock();
243 backup_p = rcu_dereference(p->backup_port);
244 if (backup_p)
245 nla_put_u32(skb, IFLA_BRPORT_BACKUP_PORT,
246 backup_p->dev->ifindex);
247 rcu_read_unlock();
248
stephen hemminger25c71c72012-11-13 07:53:05 +0000249 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800250}
251
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800252static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
253 u16 vid_end, u16 flags)
254{
255 struct bridge_vlan_info vinfo;
256
257 if ((vid_end - vid_start) > 0) {
258 /* add range to skb */
259 vinfo.vid = vid_start;
260 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
261 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
262 sizeof(vinfo), &vinfo))
263 goto nla_put_failure;
264
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800265 vinfo.vid = vid_end;
266 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
267 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
268 sizeof(vinfo), &vinfo))
269 goto nla_put_failure;
270 } else {
271 vinfo.vid = vid_start;
272 vinfo.flags = flags;
273 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
274 sizeof(vinfo), &vinfo))
275 goto nla_put_failure;
276 }
277
278 return 0;
279
280nla_put_failure:
281 return -EMSGSIZE;
282}
283
284static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200285 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800286{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200287 struct net_bridge_vlan *v;
288 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200289 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800290 int err = 0;
291
292 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
293 * and mark vlan info with begin and end flags
294 * if vlaninfo represents a range
295 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200296 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200297 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800298 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200299 if (!br_vlan_should_use(v))
300 continue;
301 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800302 flags |= BRIDGE_VLAN_INFO_PVID;
303
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200304 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800305 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
306
307 if (vid_range_start == 0) {
308 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200309 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800310 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200311 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800312 continue;
313 } else {
314 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
315 vid_range_end,
316 vid_range_flags);
317 if (err)
318 return err;
319 }
320
321initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200322 vid_range_start = v->vid;
323 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800324 vid_range_flags = flags;
325 }
326
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800327 if (vid_range_start != 0) {
328 /* Call it once more to send any left over vlans */
329 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
330 vid_range_end,
331 vid_range_flags);
332 if (err)
333 return err;
334 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800335
336 return 0;
337}
338
339static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200340 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800341{
342 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200343 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200344 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800345
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200346 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200347 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200348 if (!br_vlan_should_use(v))
349 continue;
350
351 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800352 vinfo.flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200353 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800354 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
355
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200356 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800357 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
358
359 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
360 sizeof(vinfo), &vinfo))
361 goto nla_put_failure;
362 }
363
364 return 0;
365
366nla_put_failure:
367 return -EMSGSIZE;
368}
369
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700370/*
371 * Create one netlink message for one interface
372 * Contains port and master info as well as carrier and bridge state.
373 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000374static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200375 const struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000376 u32 pid, u32 seq, int event, unsigned int flags,
377 u32 filter_mask, const struct net_device *dev)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700378{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200379 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200380 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800381 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700382 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700383
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000384 if (port)
385 br = port->br;
386 else
387 br = netdev_priv(dev);
388
stephen hemminger28a16c92010-05-10 09:31:09 +0000389 br_debug(br, "br_fill_info event %d port %s master %s\n",
390 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700391
Thomas Graf74685962006-11-20 16:20:22 -0800392 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
393 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800394 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700395
Thomas Graf74685962006-11-20 16:20:22 -0800396 hdr = nlmsg_data(nlh);
397 hdr->ifi_family = AF_BRIDGE;
398 hdr->__ifi_pad = 0;
399 hdr->ifi_type = dev->type;
400 hdr->ifi_index = dev->ifindex;
401 hdr->ifi_flags = dev_get_flags(dev);
402 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700403
David S. Miller2eb812e2012-04-01 20:49:54 -0400404 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
405 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
406 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
407 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
408 (dev->addr_len &&
409 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200410 (dev->ifindex != dev_get_iflink(dev) &&
411 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400412 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000413
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000414 if (event == RTM_NEWLINK && port) {
Michal Kubecekf78c6032019-04-26 11:13:12 +0200415 struct nlattr *nest;
stephen hemminger25c71c72012-11-13 07:53:05 +0000416
Michal Kubecekf78c6032019-04-26 11:13:12 +0200417 nest = nla_nest_start(skb, IFLA_PROTINFO);
stephen hemminger25c71c72012-11-13 07:53:05 +0000418 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
419 goto nla_put_failure;
420 nla_nest_end(skb, nest);
421 }
422
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000423 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800424 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
425 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200426 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800427 struct nlattr *af;
428 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000429
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200430 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
431 rcu_read_lock();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200432 if (port)
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200433 vg = nbp_vlan_group_rcu(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200434 else
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200435 vg = br_vlan_group_rcu(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000436
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200437 if (!vg || !vg->num_vlans) {
438 rcu_read_unlock();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000439 goto done;
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200440 }
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200441 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200442 if (!af) {
443 rcu_read_unlock();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000444 goto nla_put_failure;
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200445 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800446 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200447 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800448 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200449 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800450
451 if (port && (port->flags & BR_VLAN_TUNNEL))
452 err = br_fill_vlan_tunnel_info(skb, vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200453 rcu_read_unlock();
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800454 if (err)
455 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000456 nla_nest_end(skb, af);
457 }
458
459done:
Johannes Berg053c0952015-01-16 22:09:00 +0100460 nlmsg_end(skb, nlh);
461 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700462
Thomas Graf74685962006-11-20 16:20:22 -0800463nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800464 nlmsg_cancel(skb, nlh);
465 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700466}
467
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200468/* Notify listeners of a change in bridge or port information */
469void br_ifinfo_notify(int event, const struct net_bridge *br,
470 const struct net_bridge_port *port)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700471{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200472 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
473 struct net_device *dev;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700474 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700475 int err = -ENOBUFS;
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200476 struct net *net;
477 u16 port_no = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700478
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200479 if (WARN_ON(!port && !br))
Vlad Yasevich407af322013-02-13 12:00:12 +0000480 return;
481
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200482 if (port) {
483 dev = port->dev;
484 br = port->br;
485 port_no = port->port_no;
486 } else {
487 dev = br->dev;
488 }
stephen hemminger28a16c92010-05-10 09:31:09 +0000489
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200490 net = dev_net(dev);
491 br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event);
492
493 skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700494 if (skb == NULL)
495 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700496
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200497 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev);
Patrick McHardy26932562007-01-31 23:16:40 -0800498 if (err < 0) {
499 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
500 WARN_ON(err == -EMSGSIZE);
501 kfree_skb(skb);
502 goto errout;
503 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800504 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
505 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700506errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800507 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700508}
509
510/*
511 * Dump information about all ports, in response to GETLINK
512 */
John Fastabende5a55a82012-10-24 08:12:57 +0000513int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200514 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700515{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800516 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700517
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800518 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
519 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300520 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000521
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200522 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Dan Carpenter1b846f92015-01-21 12:22:35 +0300523 filter_mask, dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700524}
525
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800526static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata169327d2018-12-12 17:02:50 +0000527 int cmd, struct bridge_vlan_info *vinfo, bool *changed,
528 struct netlink_ext_ack *extack)
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800529{
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300530 bool curr_change;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800531 int err = 0;
532
533 switch (cmd) {
534 case RTM_SETLINK:
535 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200536 /* if the MASTER flag is set this will act on the global
537 * per-VLAN entry as well
538 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300539 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
Petr Machata169327d2018-12-12 17:02:50 +0000540 &curr_change, extack);
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800541 } else {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200542 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300543 err = br_vlan_add(br, vinfo->vid, vinfo->flags,
Petr Machata169327d2018-12-12 17:02:50 +0000544 &curr_change, extack);
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800545 }
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300546 if (curr_change)
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300547 *changed = true;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800548 break;
549
550 case RTM_DELLINK:
551 if (p) {
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300552 if (!nbp_vlan_delete(p, vinfo->vid))
553 *changed = true;
554
555 if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
556 !br_vlan_delete(p->br, vinfo->vid))
557 *changed = true;
558 } else if (!br_vlan_delete(br, vinfo->vid)) {
559 *changed = true;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800560 }
561 break;
562 }
563
564 return err;
565}
Vlad Yasevich407af322013-02-13 12:00:12 +0000566
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +0200567int br_process_vlan_info(struct net_bridge *br,
568 struct net_bridge_port *p, int cmd,
569 struct bridge_vlan_info *vinfo_curr,
570 struct bridge_vlan_info **vinfo_last,
571 bool *changed,
572 struct netlink_ext_ack *extack)
Roopa Prabhuefa53562017-01-31 22:59:54 -0800573{
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200574 int err, rtm_cmd;
575
Nikolay Aleksandrov8f4cc942020-01-14 19:56:08 +0200576 if (!br_vlan_valid_id(vinfo_curr->vid, extack))
Roopa Prabhuefa53562017-01-31 22:59:54 -0800577 return -EINVAL;
578
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200579 /* needed for vlan-only NEWVLAN/DELVLAN notifications */
580 rtm_cmd = br_afspec_cmd_to_rtm(cmd);
581
Roopa Prabhuefa53562017-01-31 22:59:54 -0800582 if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Nikolay Aleksandrov8f4cc942020-01-14 19:56:08 +0200583 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
Roopa Prabhuefa53562017-01-31 22:59:54 -0800584 return -EINVAL;
585 *vinfo_last = vinfo_curr;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800586 return 0;
587 }
588
589 if (*vinfo_last) {
590 struct bridge_vlan_info tmp_vinfo;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200591 int v, v_change_start = 0;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800592
Nikolay Aleksandrov8f4cc942020-01-14 19:56:08 +0200593 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
Roopa Prabhuefa53562017-01-31 22:59:54 -0800594 return -EINVAL;
595
596 memcpy(&tmp_vinfo, *vinfo_last,
597 sizeof(struct bridge_vlan_info));
598 for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200599 bool curr_change = false;
600
Roopa Prabhuefa53562017-01-31 22:59:54 -0800601 tmp_vinfo.vid = v;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200602 err = br_vlan_info(br, p, cmd, &tmp_vinfo, &curr_change,
Petr Machata169327d2018-12-12 17:02:50 +0000603 extack);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800604 if (err)
605 break;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200606 if (curr_change) {
607 *changed = curr_change;
608 if (!v_change_start)
609 v_change_start = v;
610 } else {
611 /* nothing to notify yet */
612 if (!v_change_start)
613 continue;
614 br_vlan_notify(br, p, v_change_start,
615 v - 1, rtm_cmd);
616 v_change_start = 0;
617 }
Roopa Prabhuefa53562017-01-31 22:59:54 -0800618 }
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200619 /* v_change_start is set only if the last/whole range changed */
620 if (v_change_start)
621 br_vlan_notify(br, p, v_change_start,
622 v - 1, rtm_cmd);
623
Roopa Prabhuefa53562017-01-31 22:59:54 -0800624 *vinfo_last = NULL;
625
Nikolay Aleksandrov66c54512017-10-19 20:17:32 +0300626 return err;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800627 }
628
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200629 err = br_vlan_info(br, p, cmd, vinfo_curr, changed, extack);
630 if (*changed)
631 br_vlan_notify(br, p, vinfo_curr->vid, 0, rtm_cmd);
632
633 return err;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800634}
635
Vlad Yasevich407af322013-02-13 12:00:12 +0000636static int br_afspec(struct net_bridge *br,
637 struct net_bridge_port *p,
638 struct nlattr *af_spec,
Petr Machata169327d2018-12-12 17:02:50 +0000639 int cmd, bool *changed,
640 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +0000641{
Roopa Prabhuefa53562017-01-31 22:59:54 -0800642 struct bridge_vlan_info *vinfo_curr = NULL;
643 struct bridge_vlan_info *vinfo_last = NULL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800644 struct nlattr *attr;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800645 struct vtunnel_info tinfo_last = {};
646 struct vtunnel_info tinfo_curr = {};
647 int err = 0, rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000648
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800649 nla_for_each_nested(attr, af_spec, rem) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800650 err = 0;
651 switch (nla_type(attr)) {
652 case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
Nikolay Aleksandrov1020ce32017-06-06 01:26:24 +0300653 if (!p || !(p->flags & BR_VLAN_TUNNEL))
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800654 return -EINVAL;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800655 err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
656 if (err)
657 return err;
658 err = br_process_vlan_tunnel_info(br, p, cmd,
659 &tinfo_curr,
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300660 &tinfo_last,
661 changed);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800662 if (err)
663 return err;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800664 break;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800665 case IFLA_BRIDGE_VLAN_INFO:
666 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
667 return -EINVAL;
668 vinfo_curr = nla_data(attr);
669 err = br_process_vlan_info(br, p, cmd, vinfo_curr,
Petr Machata169327d2018-12-12 17:02:50 +0000670 &vinfo_last, changed,
671 extack);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800672 if (err)
673 return err;
674 break;
Horatiu Vultur65369932020-04-26 15:22:07 +0200675 case IFLA_BRIDGE_MRP:
676 err = br_mrp_parse(br, p, attr, cmd, extack);
677 if (err)
678 return err;
679 break;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800680 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000681 }
682
683 return err;
684}
685
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200686static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000687 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
688 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
689 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
690 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000691 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000692 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100693 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400694 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400695 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200696 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200697 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200698 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100699 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
Tobias Klauser90512472017-05-05 16:36:53 +0200700 [IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
701 [IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrovfbec4432017-11-13 18:30:55 +0200702 [IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300703 [IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov1a3fbd32017-10-30 12:56:33 +0200704 [IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300705 [IFLA_BRPORT_ISOLATED] = { .type = NLA_U8 },
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300706 [IFLA_BRPORT_BACKUP_PORT] = { .type = NLA_U32 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000707};
708
709/* Change the state of the port and notify spanning tree */
710static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700711{
stephen hemminger25c71c72012-11-13 07:53:05 +0000712 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000713 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700714
715 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700716 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700717 return -EBUSY;
718
stephen hemminger576eb622012-12-28 18:15:22 +0000719 /* if device is not up, change is not allowed
720 * if link is not present, only allowable state is disabled
721 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000722 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000723 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700724 return -ENETDOWN;
725
Florian Fainelli775dd692014-09-30 16:13:19 -0700726 br_set_state(p, state);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000727 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700728 return 0;
729}
730
stephen hemminger25c71c72012-11-13 07:53:05 +0000731/* Set/clear or port flags based on attribute */
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200732static int br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
733 int attrtype, unsigned long mask)
stephen hemminger25c71c72012-11-13 07:53:05 +0000734{
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200735 unsigned long flags;
736 int err;
737
738 if (!tb[attrtype])
739 return 0;
740
741 if (nla_get_u8(tb[attrtype]))
742 flags = p->flags | mask;
743 else
744 flags = p->flags & ~mask;
745
746 err = br_switchdev_set_port_flag(p, flags, mask);
747 if (err)
748 return err;
749
750 p->flags = flags;
751 return 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000752}
753
754/* Process bridge protocol info on port */
755static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
756{
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400757 unsigned long old_flags = p->flags;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800758 bool br_vlan_tunnel_old = false;
759 int err;
stephen hemminger25c71c72012-11-13 07:53:05 +0000760
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200761 err = br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
762 if (err)
763 return err;
764
765 err = br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
766 if (err)
767 return err;
768
769 err = br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
770 if (err)
771 return err;
772
773 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
774 if (err)
775 return err;
776
777 err = br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
778 if (err)
779 return err;
780
781 err = br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
782 if (err)
783 return err;
784
785 err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
786 if (err)
787 return err;
788
789 err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
790 if (err)
791 return err;
792
793 err = br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
794 if (err)
795 return err;
796
797 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
798 if (err)
799 return err;
800
801 err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
802 if (err)
803 return err;
stephen hemminger25c71c72012-11-13 07:53:05 +0000804
Roopa Prabhuefa53562017-01-31 22:59:54 -0800805 br_vlan_tunnel_old = (p->flags & BR_VLAN_TUNNEL) ? true : false;
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200806 err = br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
807 if (err)
808 return err;
809
Roopa Prabhuefa53562017-01-31 22:59:54 -0800810 if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
811 nbp_vlan_tunnel_info_flush(p);
812
stephen hemminger25c71c72012-11-13 07:53:05 +0000813 if (tb[IFLA_BRPORT_COST]) {
814 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
815 if (err)
816 return err;
817 }
818
819 if (tb[IFLA_BRPORT_PRIORITY]) {
820 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
821 if (err)
822 return err;
823 }
824
825 if (tb[IFLA_BRPORT_STATE]) {
826 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
827 if (err)
828 return err;
829 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400830
Nikolay Aleksandrov9b0c6e42015-10-06 14:12:01 +0200831 if (tb[IFLA_BRPORT_FLUSH])
832 br_fdb_delete_by_port(p->br, p, 0, 0);
833
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200834#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
835 if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
836 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
837
838 err = br_multicast_set_port_router(p, mcast_router);
839 if (err)
840 return err;
841 }
842#endif
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300843
844 if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
845 u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
846
847 if (fwd_mask & BR_GROUPFWD_MACPAUSE)
848 return -EINVAL;
849 p->group_fwd_mask = fwd_mask;
850 }
851
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700852 err = br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS,
853 BR_NEIGH_SUPPRESS);
854 if (err)
855 return err;
856
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300857 err = br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
858 if (err)
859 return err;
860
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300861 if (tb[IFLA_BRPORT_BACKUP_PORT]) {
862 struct net_device *backup_dev = NULL;
863 u32 backup_ifindex;
864
865 backup_ifindex = nla_get_u32(tb[IFLA_BRPORT_BACKUP_PORT]);
866 if (backup_ifindex) {
867 backup_dev = __dev_get_by_index(dev_net(p->dev),
868 backup_ifindex);
869 if (!backup_dev)
870 return -ENOENT;
871 }
872
873 err = nbp_backup_change(p, backup_dev);
874 if (err)
875 return err;
876 }
877
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400878 br_port_flags_change(p, old_flags ^ p->flags);
stephen hemminger25c71c72012-11-13 07:53:05 +0000879 return 0;
880}
881
882/* Change state and parameters on port. */
Petr Machata2fd527b2018-12-12 17:02:48 +0000883int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags,
884 struct netlink_ext_ack *extack)
stephen hemminger25c71c72012-11-13 07:53:05 +0000885{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200886 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
887 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
888 struct net_bridge_port *p;
stephen hemminger25c71c72012-11-13 07:53:05 +0000889 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000890 struct nlattr *afspec;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300891 bool changed = false;
Scott Feldman41c498b2015-05-10 09:47:59 -0700892 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000893
Hong zhi guoc60ee672013-03-28 06:21:22 +0000894 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
895 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000896 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000897 return 0;
898
899 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000900 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800901 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000902 */
Hong zhi guo7b99a992013-03-24 03:26:47 +0000903 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000904 return -EINVAL;
905
Vlad Yasevich407af322013-02-13 12:00:12 +0000906 if (p && protinfo) {
907 if (protinfo->nla_type & NLA_F_NESTED) {
Johannes Berg8cb08172019-04-26 14:07:28 +0200908 err = nla_parse_nested_deprecated(tb, IFLA_BRPORT_MAX,
909 protinfo,
910 br_port_policy,
911 NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +0000912 if (err)
913 return err;
914
915 spin_lock_bh(&p->br->lock);
916 err = br_setport(p, tb);
917 spin_unlock_bh(&p->br->lock);
918 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -0800919 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +0000920 if (nla_len(protinfo) < sizeof(u8))
921 return -EINVAL;
922
923 spin_lock_bh(&p->br->lock);
924 err = br_set_port_state(p, nla_get_u8(protinfo));
925 spin_unlock_bh(&p->br->lock);
926 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000927 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +0000928 goto out;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300929 changed = true;
Vlad Yasevich407af322013-02-13 12:00:12 +0000930 }
stephen hemminger25c71c72012-11-13 07:53:05 +0000931
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200932 if (afspec)
Petr Machata169327d2018-12-12 17:02:50 +0000933 err = br_afspec(br, p, afspec, RTM_SETLINK, &changed, extack);
stephen hemminger25c71c72012-11-13 07:53:05 +0000934
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300935 if (changed)
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200936 br_ifinfo_notify(RTM_NEWLINK, br, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000937out:
stephen hemminger25c71c72012-11-13 07:53:05 +0000938 return err;
939}
940
Vlad Yasevich407af322013-02-13 12:00:12 +0000941/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -0800942int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +0000943{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200944 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000945 struct net_bridge_port *p;
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200946 struct nlattr *afspec;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300947 bool changed = false;
Scott Feldman85080252015-05-10 09:48:03 -0700948 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +0000949
Hong zhi guoc60ee672013-03-28 06:21:22 +0000950 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000951 if (!afspec)
952 return 0;
953
954 p = br_port_get_rtnl(dev);
955 /* We want to accept dev as bridge itself as well */
956 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
957 return -EINVAL;
958
Petr Machata169327d2018-12-12 17:02:50 +0000959 err = br_afspec(br, p, afspec, RTM_DELLINK, &changed, NULL);
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300960 if (changed)
Roopa Prabhu02dba432015-01-14 20:02:25 -0800961 /* Send RTM_NEWLINK because userspace
962 * expects RTM_NEWLINK for vlan dels
963 */
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200964 br_ifinfo_notify(RTM_NEWLINK, br, p);
Vlad Yasevich407af322013-02-13 12:00:12 +0000965
966 return err;
967}
Matthias Schiffera8b8a8892017-06-25 23:56:01 +0200968
969static int br_validate(struct nlattr *tb[], struct nlattr *data[],
970 struct netlink_ext_ack *extack)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700971{
stephen hemmingerbb900b22011-04-04 14:03:32 +0000972 if (tb[IFLA_ADDRESS]) {
973 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
974 return -EINVAL;
975 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
976 return -EADDRNOTAVAIL;
977 }
Thomas Graf32fe21c2007-03-22 11:59:03 -0700978
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900979 if (!data)
980 return 0;
981
982#ifdef CONFIG_BRIDGE_VLAN_FILTERING
983 if (data[IFLA_BR_VLAN_PROTOCOL]) {
984 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
985 case htons(ETH_P_8021Q):
986 case htons(ETH_P_8021AD):
987 break;
988 default:
989 return -EPROTONOSUPPORT;
990 }
991 }
Tobias Jungela2858602017-05-17 09:29:12 +0200992
993 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
994 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
995
996 if (defpvid >= VLAN_VID_MASK)
997 return -EINVAL;
998 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +0900999#endif
1000
Thomas Graf32fe21c2007-03-22 11:59:03 -07001001 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001002}
1003
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001004static int br_port_slave_changelink(struct net_device *brdev,
1005 struct net_device *dev,
1006 struct nlattr *tb[],
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02001007 struct nlattr *data[],
1008 struct netlink_ext_ack *extack)
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001009{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +02001010 struct net_bridge *br = netdev_priv(brdev);
1011 int ret;
1012
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001013 if (!data)
1014 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +02001015
1016 spin_lock_bh(&br->lock);
1017 ret = br_setport(br_port_get_rtnl(dev), data);
1018 spin_unlock_bh(&br->lock);
1019
1020 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001021}
1022
Jiri Pirkoced82832014-09-05 15:51:29 +02001023static int br_port_fill_slave_info(struct sk_buff *skb,
1024 const struct net_device *brdev,
1025 const struct net_device *dev)
1026{
1027 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
1028}
1029
1030static size_t br_port_get_slave_size(const struct net_device *brdev,
1031 const struct net_device *dev)
1032{
1033 return br_port_info_size();
1034}
1035
Jiri Pirko13323512014-09-05 15:51:32 +02001036static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
1037 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
1038 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
1039 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +01001040 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
1041 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
1042 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001043 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001044 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001045 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001046 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
1047 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001048 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001049 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001050 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001051 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001052 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001053 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001054 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001055 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001056 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1057 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1058 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1059 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1060 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1061 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001062 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1063 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1064 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001065 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001066 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001067 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001068 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001069 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001070 [IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001071 [IFLA_BR_MULTI_BOOLOPT] = { .type = NLA_EXACT_LEN,
1072 .len = sizeof(struct br_boolopt_multi) },
Jiri Pirko13323512014-09-05 15:51:32 +02001073};
1074
1075static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001076 struct nlattr *data[],
1077 struct netlink_ext_ack *extack)
Jiri Pirko13323512014-09-05 15:51:32 +02001078{
1079 struct net_bridge *br = netdev_priv(brdev);
1080 int err;
1081
1082 if (!data)
1083 return 0;
1084
1085 if (data[IFLA_BR_FORWARD_DELAY]) {
1086 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1087 if (err)
1088 return err;
1089 }
1090
1091 if (data[IFLA_BR_HELLO_TIME]) {
1092 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1093 if (err)
1094 return err;
1095 }
1096
1097 if (data[IFLA_BR_MAX_AGE]) {
1098 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1099 if (err)
1100 return err;
1101 }
1102
Jörg Thalheimaf615762015-03-18 10:06:58 +01001103 if (data[IFLA_BR_AGEING_TIME]) {
Scott Feldmanc62987b2015-10-08 19:23:19 -07001104 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1105 if (err)
1106 return err;
Jörg Thalheimaf615762015-03-18 10:06:58 +01001107 }
1108
1109 if (data[IFLA_BR_STP_STATE]) {
1110 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1111
1112 br_stp_set_enabled(br, stp_enabled);
1113 }
1114
1115 if (data[IFLA_BR_PRIORITY]) {
1116 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1117
1118 br_stp_set_bridge_priority(br, priority);
1119 }
1120
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001121 if (data[IFLA_BR_VLAN_FILTERING]) {
1122 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1123
1124 err = __br_vlan_filter_toggle(br, vlan_filter);
1125 if (err)
1126 return err;
1127 }
1128
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001129#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1130 if (data[IFLA_BR_VLAN_PROTOCOL]) {
1131 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1132
1133 err = __br_vlan_set_proto(br, vlan_proto);
1134 if (err)
1135 return err;
1136 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001137
1138 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1139 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1140
Petr Machata169327d2018-12-12 17:02:50 +00001141 err = __br_vlan_set_default_pvid(br, defpvid, extack);
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001142 if (err)
1143 return err;
1144 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001145
1146 if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1147 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1148
1149 err = br_vlan_set_stats(br, vlan_stats);
1150 if (err)
1151 return err;
1152 }
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001153
1154 if (data[IFLA_BR_VLAN_STATS_PER_PORT]) {
1155 __u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]);
1156
1157 err = br_vlan_set_stats_per_port(br, per_port);
1158 if (err)
1159 return err;
1160 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001161#endif
1162
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001163 if (data[IFLA_BR_GROUP_FWD_MASK]) {
1164 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1165
1166 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1167 return -EINVAL;
1168 br->group_fwd_mask = fwd_mask;
1169 }
1170
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001171 if (data[IFLA_BR_GROUP_ADDR]) {
1172 u8 new_addr[ETH_ALEN];
1173
1174 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1175 return -EINVAL;
1176 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1177 if (!is_link_local_ether_addr(new_addr))
1178 return -EINVAL;
1179 if (new_addr[5] == 1 || /* 802.3x Pause address */
1180 new_addr[5] == 2 || /* 802.3ad Slow protocols */
1181 new_addr[5] == 3) /* 802.1X PAE address */
1182 return -EINVAL;
1183 spin_lock_bh(&br->lock);
1184 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1185 spin_unlock_bh(&br->lock);
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +03001186 br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001187 br_recalculate_fwd_mask(br);
1188 }
1189
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +02001190 if (data[IFLA_BR_FDB_FLUSH])
1191 br_fdb_flush(br);
1192
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001193#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1194 if (data[IFLA_BR_MCAST_ROUTER]) {
1195 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1196
1197 err = br_multicast_set_router(br, multicast_router);
1198 if (err)
1199 return err;
1200 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001201
1202 if (data[IFLA_BR_MCAST_SNOOPING]) {
1203 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1204
YueHaibinga26d94b2018-12-17 17:46:23 +08001205 br_multicast_toggle(br, mcast_snooping);
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001206 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001207
1208 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1209 u8 val;
1210
1211 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001212 br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001213 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001214
1215 if (data[IFLA_BR_MCAST_QUERIER]) {
1216 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1217
1218 err = br_multicast_set_querier(br, mcast_querier);
1219 if (err)
1220 return err;
1221 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001222
Nikolay Aleksandrovcf332bca2018-12-05 15:14:26 +02001223 if (data[IFLA_BR_MCAST_HASH_ELASTICITY])
1224 br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
1225 RHT_ELASTICITY);
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001226
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001227 if (data[IFLA_BR_MCAST_HASH_MAX])
1228 br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001229
1230 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1231 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1232
1233 br->multicast_last_member_count = val;
1234 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001235
1236 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1237 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1238
1239 br->multicast_startup_query_count = val;
1240 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001241
1242 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1243 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1244
1245 br->multicast_last_member_interval = clock_t_to_jiffies(val);
1246 }
1247
1248 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1249 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1250
1251 br->multicast_membership_interval = clock_t_to_jiffies(val);
1252 }
1253
1254 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1255 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1256
1257 br->multicast_querier_interval = clock_t_to_jiffies(val);
1258 }
1259
1260 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1261 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1262
1263 br->multicast_query_interval = clock_t_to_jiffies(val);
1264 }
1265
1266 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1267 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1268
1269 br->multicast_query_response_interval = clock_t_to_jiffies(val);
1270 }
1271
1272 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1273 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1274
1275 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
1276 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001277
1278 if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1279 __u8 mcast_stats;
1280
1281 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001282 br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001283 }
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001284
1285 if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1286 __u8 igmp_version;
1287
1288 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1289 err = br_multicast_set_igmp_version(br, igmp_version);
1290 if (err)
1291 return err;
1292 }
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001293
1294#if IS_ENABLED(CONFIG_IPV6)
1295 if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1296 __u8 mld_version;
1297
1298 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1299 err = br_multicast_set_mld_version(br, mld_version);
1300 if (err)
1301 return err;
1302 }
1303#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001304#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001305#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1306 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1307 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1308
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001309 br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001310 }
1311
1312 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1313 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1314
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001315 br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001316 }
1317
1318 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1319 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1320
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001321 br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001322 }
1323#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001324
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001325 if (data[IFLA_BR_MULTI_BOOLOPT]) {
1326 struct br_boolopt_multi *bm;
1327
1328 bm = nla_data(data[IFLA_BR_MULTI_BOOLOPT]);
1329 err = br_boolopt_multi_toggle(br, bm, extack);
1330 if (err)
1331 return err;
1332 }
1333
Jiri Pirko13323512014-09-05 15:51:32 +02001334 return 0;
1335}
1336
Ivan Vecerab6677442017-01-20 18:12:17 +01001337static int br_dev_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001338 struct nlattr *tb[], struct nlattr *data[],
1339 struct netlink_ext_ack *extack)
Ivan Vecerab6677442017-01-20 18:12:17 +01001340{
1341 struct net_bridge *br = netdev_priv(dev);
1342 int err;
1343
Nikolay Aleksandrov84aeb432017-12-18 17:35:09 +02001344 err = register_netdevice(dev);
1345 if (err)
1346 return err;
1347
Ivan Vecerab6677442017-01-20 18:12:17 +01001348 if (tb[IFLA_ADDRESS]) {
1349 spin_lock_bh(&br->lock);
1350 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1351 spin_unlock_bh(&br->lock);
1352 }
1353
Matthias Schifferad744b22017-06-25 23:56:00 +02001354 err = br_changelink(dev, tb, data, extack);
Ido Schimmel5b8d5422017-04-10 14:59:28 +03001355 if (err)
Nikolay Aleksandrov84aeb432017-12-18 17:35:09 +02001356 br_dev_delete(dev, NULL);
1357
Ido Schimmel5b8d5422017-04-10 14:59:28 +03001358 return err;
Ivan Vecerab6677442017-01-20 18:12:17 +01001359}
1360
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001361static size_t br_get_size(const struct net_device *brdev)
1362{
1363 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1364 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1365 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001366 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1367 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1368 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001369 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001370#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1371 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001372 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001373 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001374 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_PER_PORT */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001375#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001376 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001377 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001378 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001379 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001380 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001381 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1382 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001383 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1384 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1385 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1386 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001387 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001388#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1389 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001390 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001391 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001392 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001393 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001394 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1395 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1396 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1397 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001398 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1399 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1400 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1401 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1402 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1403 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001404 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001405 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001406#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001407#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1408 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1409 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1410 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1411#endif
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001412 nla_total_size(sizeof(struct br_boolopt_multi)) + /* IFLA_BR_MULTI_BOOLOPT */
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001413 0;
1414}
1415
1416static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1417{
1418 struct net_bridge *br = netdev_priv(brdev);
1419 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1420 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1421 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001422 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1423 u32 stp_enabled = br->stp_enabled;
1424 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Ido Schimmel1f514452017-05-26 08:37:23 +02001425 u8 vlan_enabled = br_vlan_enabled(br->dev);
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001426 struct br_boolopt_multi bm;
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001427 u64 clockval;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001428
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001429 clockval = br_timer_value(&br->hello_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001430 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001431 return -EMSGSIZE;
1432 clockval = br_timer_value(&br->tcn_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001433 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001434 return -EMSGSIZE;
1435 clockval = br_timer_value(&br->topology_change_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001436 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1437 IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001438 return -EMSGSIZE;
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +01001439 clockval = br_timer_value(&br->gc_work.timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001440 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001441 return -EMSGSIZE;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001442
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001443 br_boolopt_multi_get(br, &bm);
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001444 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1445 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001446 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1447 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1448 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001449 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001450 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001451 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1452 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1453 &br->bridge_id) ||
1454 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1455 &br->designated_root) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001456 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001457 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1458 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1459 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001460 br->topology_change_detected) ||
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001461 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) ||
1462 nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001463 return -EMSGSIZE;
1464
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001465#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001466 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001467 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001468 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED,
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001469 br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) ||
1470 nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT,
Nikolay Aleksandrov600bea72019-04-16 16:15:56 +03001471 br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001472 return -EMSGSIZE;
1473#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001474#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001475 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03001476 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING,
1477 br_opt_get(br, BROPT_MULTICAST_ENABLED)) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001478 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001479 br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) ||
1480 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER,
1481 br_opt_get(br, BROPT_MULTICAST_QUERIER)) ||
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001482 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001483 br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
Nikolay Aleksandrovcf332bca2018-12-05 15:14:26 +02001484 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001485 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1486 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001487 br->multicast_last_member_count) ||
1488 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001489 br->multicast_startup_query_count) ||
1490 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1491 br->multicast_igmp_version))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001492 return -EMSGSIZE;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001493#if IS_ENABLED(CONFIG_IPV6)
1494 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1495 br->multicast_mld_version))
1496 return -EMSGSIZE;
1497#endif
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001498 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001499 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1500 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001501 return -EMSGSIZE;
1502 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001503 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1504 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001505 return -EMSGSIZE;
1506 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001507 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1508 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001509 return -EMSGSIZE;
1510 clockval = jiffies_to_clock_t(br->multicast_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001511 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1512 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001513 return -EMSGSIZE;
1514 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001515 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1516 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001517 return -EMSGSIZE;
1518 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001519 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1520 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001521 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001522#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001523#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1524 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001525 br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) ||
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001526 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001527 br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) ||
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001528 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001529 br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0))
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001530 return -EMSGSIZE;
1531#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001532
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001533 return 0;
1534}
1535
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001536static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001537{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001538 struct net_bridge_port *p = NULL;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001539 struct net_bridge_vlan_group *vg;
1540 struct net_bridge_vlan *v;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001541 struct net_bridge *br;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001542 int numvls = 0;
1543
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001544 switch (attr) {
1545 case IFLA_STATS_LINK_XSTATS:
1546 br = netdev_priv(dev);
1547 vg = br_vlan_group(br);
1548 break;
1549 case IFLA_STATS_LINK_XSTATS_SLAVE:
1550 p = br_port_get_rtnl(dev);
1551 if (!p)
1552 return 0;
1553 br = p->br;
1554 vg = nbp_vlan_group(p);
1555 break;
1556 default:
1557 return 0;
1558 }
1559
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001560 if (vg) {
1561 /* we need to count all, even placeholder entries */
1562 list_for_each_entry(v, &vg->vlan_list, vlist)
1563 numvls++;
1564 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001565
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001566 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001567 nla_total_size(sizeof(struct br_mcast_stats)) +
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001568 nla_total_size(0);
1569}
1570
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001571static int br_fill_linkxstats(struct sk_buff *skb,
1572 const struct net_device *dev,
1573 int *prividx, int attr)
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001574{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001575 struct nlattr *nla __maybe_unused;
1576 struct net_bridge_port *p = NULL;
1577 struct net_bridge_vlan_group *vg;
1578 struct net_bridge_vlan *v;
1579 struct net_bridge *br;
1580 struct nlattr *nest;
1581 int vl_idx = 0;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001582
1583 switch (attr) {
1584 case IFLA_STATS_LINK_XSTATS:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001585 br = netdev_priv(dev);
1586 vg = br_vlan_group(br);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001587 break;
1588 case IFLA_STATS_LINK_XSTATS_SLAVE:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001589 p = br_port_get_rtnl(dev);
1590 if (!p)
1591 return 0;
1592 br = p->br;
1593 vg = nbp_vlan_group(p);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001594 break;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001595 default:
1596 return -EINVAL;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001597 }
1598
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001599 nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE);
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001600 if (!nest)
1601 return -EMSGSIZE;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001602
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001603 if (vg) {
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001604 u16 pvid;
1605
1606 pvid = br_get_pvid(vg);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001607 list_for_each_entry(v, &vg->vlan_list, vlist) {
1608 struct bridge_vlan_xstats vxi;
1609 struct br_vlan_stats stats;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001610
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001611 if (++vl_idx < *prividx)
1612 continue;
1613 memset(&vxi, 0, sizeof(vxi));
1614 vxi.vid = v->vid;
Nikolay Aleksandrov61ba1a22016-08-17 12:53:10 +02001615 vxi.flags = v->flags;
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001616 if (v->vid == pvid)
1617 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001618 br_vlan_get_stats(v, &stats);
1619 vxi.rx_bytes = stats.rx_bytes;
1620 vxi.rx_packets = stats.rx_packets;
1621 vxi.tx_bytes = stats.tx_bytes;
1622 vxi.tx_packets = stats.tx_packets;
1623
1624 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1625 goto nla_put_failure;
1626 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001627 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001628
1629#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1630 if (++vl_idx >= *prividx) {
1631 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1632 sizeof(struct br_mcast_stats),
1633 BRIDGE_XSTATS_PAD);
1634 if (!nla)
1635 goto nla_put_failure;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001636 br_multicast_get_stats(br, p, nla_data(nla));
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001637 }
1638#endif
Vivien Didelotde179962019-12-11 20:07:10 -05001639
1640 if (p) {
1641 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_STP,
1642 sizeof(p->stp_xstats),
1643 BRIDGE_XSTATS_PAD);
1644 if (!nla)
1645 goto nla_put_failure;
1646
1647 spin_lock_bh(&br->lock);
1648 memcpy(nla_data(nla), &p->stp_xstats, sizeof(p->stp_xstats));
1649 spin_unlock_bh(&br->lock);
1650 }
1651
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001652 nla_nest_end(skb, nest);
1653 *prividx = 0;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001654
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001655 return 0;
1656
1657nla_put_failure:
1658 nla_nest_end(skb, nest);
1659 *prividx = vl_idx;
1660
1661 return -EMSGSIZE;
1662}
Roopa Prabhufed0a152015-02-25 23:55:40 -08001663
Daniel Borkmann207895fd32015-01-29 12:15:03 +01001664static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001665 .family = AF_BRIDGE,
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001666 .get_link_af_size = br_get_link_af_size_filtered,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001667};
1668
stephen hemminger149ddd82012-06-26 05:48:45 +00001669struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001670 .kind = "bridge",
1671 .priv_size = sizeof(struct net_bridge),
1672 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001673 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001674 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001675 .validate = br_validate,
1676 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001677 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001678 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001679 .get_size = br_get_size,
1680 .fill_info = br_fill_info,
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001681 .fill_linkxstats = br_fill_linkxstats,
1682 .get_linkxstats_size = br_get_linkxstats_size,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001683
1684 .slave_maxtype = IFLA_BRPORT_MAX,
1685 .slave_policy = br_port_policy,
1686 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001687 .get_slave_size = br_port_get_slave_size,
1688 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001689};
1690
1691int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001692{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001693 int err;
1694
1695 br_mdb_init();
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001696 br_vlan_rtnl_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001697 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001698
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001699 err = rtnl_link_register(&br_link_ops);
1700 if (err)
1701 goto out_af;
1702
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001703 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001704
1705out_af:
1706 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001707 br_mdb_uninit();
1708 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001709}
1710
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001711void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001712{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001713 br_mdb_uninit();
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001714 br_vlan_rtnl_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001715 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001716 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001717}