blob: 2ff83d84230d3136747dbd12507d071855b53cf6 [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"
Henrik Bjoernlund5e312fc2020-10-27 10:02:49 +000019#include "br_private_cfm.h"
Roopa Prabhuefa53562017-01-31 22:59:54 -080020#include "br_private_tunnel.h"
Nikolay Aleksandrov2dba4072021-01-26 11:35:33 +020021#include "br_private_mcast_eht.h"
Stephen Hemminger11dc1f32006-05-25 16:00:12 -070022
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020023static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020024 u32 filter_mask)
Roopa Prabhufed0a152015-02-25 23:55:40 -080025{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020026 struct net_bridge_vlan *v;
27 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020028 u16 flags, pvid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080029 int num_vlans = 0;
30
Roopa Prabhufed0a152015-02-25 23:55:40 -080031 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
32 return 0;
33
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020034 pvid = br_get_pvid(vg);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020035 /* Count number of vlan infos */
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020036 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Roopa Prabhufed0a152015-02-25 23:55:40 -080037 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020038 /* only a context, bridge vlan not activated */
39 if (!br_vlan_should_use(v))
40 continue;
41 if (v->vid == pvid)
Roopa Prabhufed0a152015-02-25 23:55:40 -080042 flags |= BRIDGE_VLAN_INFO_PVID;
43
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020044 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhufed0a152015-02-25 23:55:40 -080045 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
46
47 if (vid_range_start == 0) {
48 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020049 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhufed0a152015-02-25 23:55:40 -080050 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020051 vid_range_end = v->vid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080052 continue;
53 } else {
54 if ((vid_range_end - vid_range_start) > 0)
55 num_vlans += 2;
56 else
57 num_vlans += 1;
58 }
59initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020060 vid_range_start = v->vid;
61 vid_range_end = v->vid;
Roopa Prabhufed0a152015-02-25 23:55:40 -080062 vid_range_flags = flags;
63 }
64
65 if (vid_range_start != 0) {
66 if ((vid_range_end - vid_range_start) > 0)
67 num_vlans += 2;
68 else
69 num_vlans += 1;
70 }
71
72 return num_vlans;
73}
74
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020075static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +020076 u32 filter_mask)
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020077{
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020078 int num_vlans;
79
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020080 if (!vg)
81 return 0;
82
83 if (filter_mask & RTEXT_FILTER_BRVLAN)
84 return vg->num_vlans;
85
Nikolay Aleksandrov586c2b52015-10-02 15:05:10 +020086 rcu_read_lock();
87 num_vlans = __get_num_vlan_infos(vg, filter_mask);
88 rcu_read_unlock();
89
90 return num_vlans;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020091}
92
Roopa Prabhufed0a152015-02-25 23:55:40 -080093static size_t br_get_link_af_size_filtered(const struct net_device *dev,
94 u32 filter_mask)
Roopa Prabhub7853d72015-02-21 20:21:51 -080095{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +020096 struct net_bridge_vlan_group *vg = NULL;
Roopa Prabhuefa53562017-01-31 22:59:54 -080097 struct net_bridge_port *p = NULL;
Henrik Bjoernlundb6d04252020-10-27 10:02:51 +000098 struct net_bridge *br = NULL;
99 u32 num_cfm_peer_mep_infos;
100 u32 num_cfm_mep_infos;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800101 size_t vinfo_sz = 0;
Henrik Bjoernlundb6d04252020-10-27 10:02:51 +0000102 int num_vlan_infos;
Roopa Prabhub7853d72015-02-21 20:21:51 -0800103
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100104 rcu_read_lock();
Julian Wiedmann35f861e2019-03-29 14:38:19 +0100105 if (netif_is_bridge_port(dev)) {
Zhang Zhengming59259ff2021-04-28 22:38:14 +0800106 p = br_port_get_check_rcu(dev);
107 if (p)
108 vg = nbp_vlan_group_rcu(p);
Kyungrok Chung254ec032021-10-16 20:21:36 +0900109 } else if (netif_is_bridge_master(dev)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200110 br = netdev_priv(dev);
Nikolay Aleksandrov907b1e62015-10-12 21:47:02 +0200111 vg = br_vlan_group_rcu(br);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200112 }
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200113 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
Johannes Berg2f56f6b2015-03-03 16:02:16 +0100114 rcu_read_unlock();
Roopa Prabhub7853d72015-02-21 20:21:51 -0800115
Roopa Prabhuefa53562017-01-31 22:59:54 -0800116 if (p && (p->flags & BR_VLAN_TUNNEL))
117 vinfo_sz += br_get_vlan_tunnel_info_size(vg);
118
Roopa Prabhub7853d72015-02-21 20:21:51 -0800119 /* Each VLAN is returned in bridge_vlan_info along with flags */
Roopa Prabhuefa53562017-01-31 22:59:54 -0800120 vinfo_sz += num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
121
Henrik Bjoernlundb6d04252020-10-27 10:02:51 +0000122 if (!(filter_mask & RTEXT_FILTER_CFM_STATUS))
123 return vinfo_sz;
124
125 if (!br)
126 return vinfo_sz;
127
128 /* CFM status info must be added */
129 br_cfm_mep_count(br, &num_cfm_mep_infos);
130 br_cfm_peer_mep_count(br, &num_cfm_peer_mep_infos);
131
132 vinfo_sz += nla_total_size(0); /* IFLA_BRIDGE_CFM */
133 /* For each status struct the MEP instance (u32) is added */
134 /* MEP instance (u32) + br_cfm_mep_status */
135 vinfo_sz += num_cfm_mep_infos *
136 /*IFLA_BRIDGE_CFM_MEP_STATUS_INSTANCE */
137 (nla_total_size(sizeof(u32))
138 /* IFLA_BRIDGE_CFM_MEP_STATUS_OPCODE_UNEXP_SEEN */
139 + nla_total_size(sizeof(u32))
140 /* IFLA_BRIDGE_CFM_MEP_STATUS_VERSION_UNEXP_SEEN */
141 + nla_total_size(sizeof(u32))
142 /* IFLA_BRIDGE_CFM_MEP_STATUS_RX_LEVEL_LOW_SEEN */
143 + nla_total_size(sizeof(u32)));
144 /* MEP instance (u32) + br_cfm_cc_peer_status */
145 vinfo_sz += num_cfm_peer_mep_infos *
146 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_INSTANCE */
147 (nla_total_size(sizeof(u32))
148 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_PEER_MEPID */
149 + nla_total_size(sizeof(u32))
150 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_CCM_DEFECT */
151 + nla_total_size(sizeof(u32))
152 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_RDI */
153 + nla_total_size(sizeof(u32))
154 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_PORT_TLV_VALUE */
155 + nla_total_size(sizeof(u8))
156 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_IF_TLV_VALUE */
157 + nla_total_size(sizeof(u8))
158 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_SEEN */
159 + nla_total_size(sizeof(u32))
160 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_TLV_SEEN */
161 + nla_total_size(sizeof(u32))
162 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_SEQ_UNEXP_SEEN */
163 + nla_total_size(sizeof(u32)));
164
Roopa Prabhuefa53562017-01-31 22:59:54 -0800165 return vinfo_sz;
Roopa Prabhub7853d72015-02-21 20:21:51 -0800166}
167
stephen hemminger25c71c72012-11-13 07:53:05 +0000168static inline size_t br_port_info_size(void)
169{
170 return nla_total_size(1) /* IFLA_BRPORT_STATE */
171 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
172 + nla_total_size(4) /* IFLA_BRPORT_COST */
173 + nla_total_size(1) /* IFLA_BRPORT_MODE */
stephen hemmingera2e01a62012-11-13 07:53:07 +0000174 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
stephen hemminger1007dd12012-11-13 07:53:08 +0000175 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
stephen hemminger3da889b2013-03-11 13:52:17 +0000176 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100177 + nla_total_size(1) /* IFLA_BRPORT_MCAST_TO_UCAST */
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400178 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
Vlad Yasevich867a5942013-06-05 10:08:01 -0400179 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
Tobias Klauser90512472017-05-05 16:36:53 +0200180 + nla_total_size(1) /* IFLA_BRPORT_MCAST_FLOOD */
181 + nla_total_size(1) /* IFLA_BRPORT_BCAST_FLOOD */
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200182 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200183 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
Roopa Prabhuefa53562017-01-31 22:59:54 -0800184 + nla_total_size(1) /* IFLA_BRPORT_VLAN_TUNNEL */
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700185 + nla_total_size(1) /* IFLA_BRPORT_NEIGH_SUPPRESS */
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300186 + nla_total_size(1) /* IFLA_BRPORT_ISOLATED */
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200187 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200188 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200189 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
190 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200191 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
192 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200193 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
194 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200195 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
196 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
197 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200198#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
199 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
200#endif
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300201 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_GROUP_FWD_MASK */
Horatiu Vultur3e544422020-04-26 15:22:01 +0200202 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MRP_RING_OPEN */
Horatiu Vulturffb3adb2020-07-14 09:34:58 +0200203 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MRP_IN_OPEN */
Nikolay Aleksandrov2dba4072021-01-26 11:35:33 +0200204 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT */
205 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_EHT_HOSTS_CNT */
stephen hemminger25c71c72012-11-13 07:53:05 +0000206 + 0;
207}
208
Roopa Prabhufed0a152015-02-25 23:55:40 -0800209static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
Thomas Graf339bf982006-11-10 14:10:15 -0800210{
211 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
stephen hemminger25c71c72012-11-13 07:53:05 +0000212 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
213 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
214 + nla_total_size(4) /* IFLA_MASTER */
215 + nla_total_size(4) /* IFLA_MTU */
216 + nla_total_size(4) /* IFLA_LINK */
217 + nla_total_size(1) /* IFLA_OPERSTATE */
Roopa Prabhub7853d72015-02-21 20:21:51 -0800218 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
Roopa Prabhufed0a152015-02-25 23:55:40 -0800219 + nla_total_size(br_get_link_af_size_filtered(dev,
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300220 filter_mask)) /* IFLA_AF_SPEC */
221 + nla_total_size(4); /* IFLA_BRPORT_BACKUP_PORT */
stephen hemminger25c71c72012-11-13 07:53:05 +0000222}
223
224static int br_port_fill_attrs(struct sk_buff *skb,
225 const struct net_bridge_port *p)
226{
227 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300228 struct net_bridge_port *backup_p;
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200229 u64 timerval;
stephen hemminger25c71c72012-11-13 07:53:05 +0000230
231 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
232 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
233 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
stephen hemmingera2e01a62012-11-13 07:53:07 +0000234 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
stephen hemminger1007dd12012-11-13 07:53:08 +0000235 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200236 nla_put_u8(skb, IFLA_BRPORT_PROTECT,
237 !!(p->flags & BR_ROOT_BLOCK)) ||
238 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
239 !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100240 nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
241 !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
Vlad Yasevich867a5942013-06-05 10:08:01 -0400242 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
Nikolay Aleksandrovb6cb5ac2016-08-31 15:36:52 +0200243 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
244 !!(p->flags & BR_FLOOD)) ||
245 nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
246 !!(p->flags & BR_MCAST_FLOOD)) ||
Mike Manning99f906e2017-04-26 14:48:09 +0100247 nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
248 !!(p->flags & BR_BCAST_FLOOD)) ||
Jouni Malinen842a9ae2015-03-04 12:54:21 +0200249 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
250 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
Nikolay Aleksandrov4ebc7662015-10-06 14:11:55 +0200251 !!(p->flags & BR_PROXYARP_WIFI)) ||
252 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov80df9a22015-10-06 14:11:56 +0200253 &p->designated_root) ||
254 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
Nikolay Aleksandrov96f94e72015-10-06 14:11:57 +0200255 &p->designated_bridge) ||
256 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
Nikolay Aleksandrov42d452c2015-10-06 14:11:58 +0200257 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
258 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
Nikolay Aleksandrove08e8382015-10-06 14:11:59 +0200259 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
260 nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
261 p->topology_change_ack) ||
Roopa Prabhuefa53562017-01-31 22:59:54 -0800262 nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
263 nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300264 BR_VLAN_TUNNEL)) ||
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700265 nla_put_u16(skb, IFLA_BRPORT_GROUP_FWD_MASK, p->group_fwd_mask) ||
266 nla_put_u8(skb, IFLA_BRPORT_NEIGH_SUPPRESS,
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300267 !!(p->flags & BR_NEIGH_SUPPRESS)) ||
Horatiu Vultur3e544422020-04-26 15:22:01 +0200268 nla_put_u8(skb, IFLA_BRPORT_MRP_RING_OPEN, !!(p->flags &
269 BR_MRP_LOST_CONT)) ||
Horatiu Vulturffb3adb2020-07-14 09:34:58 +0200270 nla_put_u8(skb, IFLA_BRPORT_MRP_IN_OPEN,
271 !!(p->flags & BR_MRP_LOST_IN_CONT)) ||
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300272 nla_put_u8(skb, IFLA_BRPORT_ISOLATED, !!(p->flags & BR_ISOLATED)))
stephen hemminger25c71c72012-11-13 07:53:05 +0000273 return -EMSGSIZE;
274
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200275 timerval = br_timer_value(&p->message_age_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200276 if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
277 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200278 return -EMSGSIZE;
279 timerval = br_timer_value(&p->forward_delay_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200280 if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
281 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200282 return -EMSGSIZE;
283 timerval = br_timer_value(&p->hold_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +0200284 if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
285 IFLA_BRPORT_PAD))
Nikolay Aleksandrov61c0a9a2015-10-06 14:12:00 +0200286 return -EMSGSIZE;
287
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200288#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
289 if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
Nikolay Aleksandrov96322332021-07-19 20:06:23 +0300290 p->multicast_ctx.multicast_router) ||
Nikolay Aleksandrov2dba4072021-01-26 11:35:33 +0200291 nla_put_u32(skb, IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT,
292 p->multicast_eht_hosts_limit) ||
293 nla_put_u32(skb, IFLA_BRPORT_MCAST_EHT_HOSTS_CNT,
294 p->multicast_eht_hosts_cnt))
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200295 return -EMSGSIZE;
296#endif
297
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300298 /* we might be called only with br->lock */
299 rcu_read_lock();
300 backup_p = rcu_dereference(p->backup_port);
301 if (backup_p)
302 nla_put_u32(skb, IFLA_BRPORT_BACKUP_PORT,
303 backup_p->dev->ifindex);
304 rcu_read_unlock();
305
stephen hemminger25c71c72012-11-13 07:53:05 +0000306 return 0;
Thomas Graf339bf982006-11-10 14:10:15 -0800307}
308
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800309static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
310 u16 vid_end, u16 flags)
311{
312 struct bridge_vlan_info vinfo;
313
314 if ((vid_end - vid_start) > 0) {
315 /* add range to skb */
316 vinfo.vid = vid_start;
317 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
318 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
319 sizeof(vinfo), &vinfo))
320 goto nla_put_failure;
321
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800322 vinfo.vid = vid_end;
323 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
324 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
325 sizeof(vinfo), &vinfo))
326 goto nla_put_failure;
327 } else {
328 vinfo.vid = vid_start;
329 vinfo.flags = flags;
330 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
331 sizeof(vinfo), &vinfo))
332 goto nla_put_failure;
333 }
334
335 return 0;
336
337nla_put_failure:
338 return -EMSGSIZE;
339}
340
341static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200342 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800343{
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200344 struct net_bridge_vlan *v;
345 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200346 u16 flags, pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800347 int err = 0;
348
349 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
350 * and mark vlan info with begin and end flags
351 * if vlaninfo represents a range
352 */
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200353 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200354 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800355 flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200356 if (!br_vlan_should_use(v))
357 continue;
358 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800359 flags |= BRIDGE_VLAN_INFO_PVID;
360
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200361 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800362 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
363
364 if (vid_range_start == 0) {
365 goto initvars;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200366 } else if ((v->vid - vid_range_end) == 1 &&
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800367 flags == vid_range_flags) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200368 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800369 continue;
370 } else {
371 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
372 vid_range_end,
373 vid_range_flags);
374 if (err)
375 return err;
376 }
377
378initvars:
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200379 vid_range_start = v->vid;
380 vid_range_end = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800381 vid_range_flags = flags;
382 }
383
Roopa Prabhu0fe6de42015-01-12 16:25:28 -0800384 if (vid_range_start != 0) {
385 /* Call it once more to send any left over vlans */
386 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
387 vid_range_end,
388 vid_range_flags);
389 if (err)
390 return err;
391 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800392
393 return 0;
394}
395
396static int br_fill_ifvlaninfo(struct sk_buff *skb,
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200397 struct net_bridge_vlan_group *vg)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800398{
399 struct bridge_vlan_info vinfo;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200400 struct net_bridge_vlan *v;
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200401 u16 pvid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800402
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200403 pvid = br_get_pvid(vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200404 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200405 if (!br_vlan_should_use(v))
406 continue;
407
408 vinfo.vid = v->vid;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800409 vinfo.flags = 0;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200410 if (v->vid == pvid)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800411 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
412
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200413 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800414 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
415
416 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
417 sizeof(vinfo), &vinfo))
418 goto nla_put_failure;
419 }
420
421 return 0;
422
423nla_put_failure:
424 return -EMSGSIZE;
425}
426
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700427/*
428 * Create one netlink message for one interface
429 * Contains port and master info as well as carrier and bridge state.
430 */
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000431static int br_fill_ifinfo(struct sk_buff *skb,
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200432 const struct net_bridge_port *port,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000433 u32 pid, u32 seq, int event, unsigned int flags,
Henrik Bjoernlundb6d04252020-10-27 10:02:51 +0000434 u32 filter_mask, const struct net_device *dev,
435 bool getlink)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700436{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200437 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
Henrik Bjoernlundb6c02ef2020-10-07 12:07:00 +0000438 struct nlattr *af = NULL;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200439 struct net_bridge *br;
Thomas Graf74685962006-11-20 16:20:22 -0800440 struct ifinfomsg *hdr;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700441 struct nlmsghdr *nlh;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700442
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000443 if (port)
444 br = port->br;
445 else
446 br = netdev_priv(dev);
447
stephen hemminger28a16c92010-05-10 09:31:09 +0000448 br_debug(br, "br_fill_info event %d port %s master %s\n",
449 event, dev->name, br->dev->name);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700450
Thomas Graf74685962006-11-20 16:20:22 -0800451 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
452 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800453 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700454
Thomas Graf74685962006-11-20 16:20:22 -0800455 hdr = nlmsg_data(nlh);
456 hdr->ifi_family = AF_BRIDGE;
457 hdr->__ifi_pad = 0;
458 hdr->ifi_type = dev->type;
459 hdr->ifi_index = dev->ifindex;
460 hdr->ifi_flags = dev_get_flags(dev);
461 hdr->ifi_change = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700462
David S. Miller2eb812e2012-04-01 20:49:54 -0400463 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
464 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
465 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
466 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
467 (dev->addr_len &&
468 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200469 (dev->ifindex != dev_get_iflink(dev) &&
470 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Miller2eb812e2012-04-01 20:49:54 -0400471 goto nla_put_failure;
stephen hemminger25c71c72012-11-13 07:53:05 +0000472
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000473 if (event == RTM_NEWLINK && port) {
Michal Kubecekf78c6032019-04-26 11:13:12 +0200474 struct nlattr *nest;
stephen hemminger25c71c72012-11-13 07:53:05 +0000475
Michal Kubecekf78c6032019-04-26 11:13:12 +0200476 nest = nla_nest_start(skb, IFLA_PROTINFO);
stephen hemminger25c71c72012-11-13 07:53:05 +0000477 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
478 goto nla_put_failure;
479 nla_nest_end(skb, nest);
480 }
481
Henrik Bjoernlundb6c02ef2020-10-07 12:07:00 +0000482 if (filter_mask & (RTEXT_FILTER_BRVLAN |
483 RTEXT_FILTER_BRVLAN_COMPRESSED |
Henrik Bjoernlund5e312fc2020-10-27 10:02:49 +0000484 RTEXT_FILTER_MRP |
Henrik Bjoernlunde77824d2020-10-27 10:02:50 +0000485 RTEXT_FILTER_CFM_CONFIG |
486 RTEXT_FILTER_CFM_STATUS)) {
Henrik Bjoernlundb6c02ef2020-10-07 12:07:00 +0000487 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
488 if (!af)
489 goto nla_put_failure;
490 }
491
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000492 /* Check if the VID information is requested */
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800493 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
494 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200495 struct net_bridge_vlan_group *vg;
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800496 int err;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000497
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200498 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
499 rcu_read_lock();
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200500 if (port)
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200501 vg = nbp_vlan_group_rcu(port);
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200502 else
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200503 vg = br_vlan_group_rcu(br);
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000504
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200505 if (!vg || !vg->num_vlans) {
506 rcu_read_unlock();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000507 goto done;
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200508 }
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800509 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200510 err = br_fill_ifvlaninfo_compressed(skb, vg);
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800511 else
Nikolay Aleksandrov77751ee2015-09-30 20:16:53 +0200512 err = br_fill_ifvlaninfo(skb, vg);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800513
514 if (port && (port->flags & BR_VLAN_TUNNEL))
515 err = br_fill_vlan_tunnel_info(skb, vg);
Nikolay Aleksandrove9c953e2015-10-12 21:47:03 +0200516 rcu_read_unlock();
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800517 if (err)
518 goto nla_put_failure;
Horatiu Vultur36a8e8e2020-07-02 10:13:07 +0200519 }
520
521 if (filter_mask & RTEXT_FILTER_MRP) {
Horatiu Vultur36a8e8e2020-07-02 10:13:07 +0200522 int err;
523
524 if (!br_mrp_enabled(br) || port)
525 goto done;
526
Horatiu Vultur36a8e8e2020-07-02 10:13:07 +0200527 rcu_read_lock();
528 err = br_mrp_fill_info(skb, br);
529 rcu_read_unlock();
530
531 if (err)
532 goto nla_put_failure;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000533 }
534
Henrik Bjoernlunde77824d2020-10-27 10:02:50 +0000535 if (filter_mask & (RTEXT_FILTER_CFM_CONFIG | RTEXT_FILTER_CFM_STATUS)) {
Henrik Bjoernlund5e312fc2020-10-27 10:02:49 +0000536 struct nlattr *cfm_nest = NULL;
537 int err;
538
539 if (!br_cfm_created(br) || port)
540 goto done;
541
542 cfm_nest = nla_nest_start(skb, IFLA_BRIDGE_CFM);
543 if (!cfm_nest)
544 goto nla_put_failure;
545
546 if (filter_mask & RTEXT_FILTER_CFM_CONFIG) {
547 rcu_read_lock();
548 err = br_cfm_config_fill_info(skb, br);
549 rcu_read_unlock();
550 if (err)
551 goto nla_put_failure;
552 }
553
Henrik Bjoernlunde77824d2020-10-27 10:02:50 +0000554 if (filter_mask & RTEXT_FILTER_CFM_STATUS) {
555 rcu_read_lock();
Henrik Bjoernlundb6d04252020-10-27 10:02:51 +0000556 err = br_cfm_status_fill_info(skb, br, getlink);
Henrik Bjoernlunde77824d2020-10-27 10:02:50 +0000557 rcu_read_unlock();
558 if (err)
559 goto nla_put_failure;
560 }
561
Henrik Bjoernlund5e312fc2020-10-27 10:02:49 +0000562 nla_nest_end(skb, cfm_nest);
563 }
564
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +0000565done:
Henrik Bjoernlundb6c02ef2020-10-07 12:07:00 +0000566 if (af)
567 nla_nest_end(skb, af);
Johannes Berg053c0952015-01-16 22:09:00 +0100568 nlmsg_end(skb, nlh);
569 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700570
Thomas Graf74685962006-11-20 16:20:22 -0800571nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800572 nlmsg_cancel(skb, nlh);
573 return -EMSGSIZE;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700574}
575
Henrik Bjoernlundb6d04252020-10-27 10:02:51 +0000576void br_info_notify(int event, const struct net_bridge *br,
577 const struct net_bridge_port *port, u32 filter)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700578{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200579 struct net_device *dev;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700580 struct sk_buff *skb;
Thomas Graf280a3062006-08-15 00:36:28 -0700581 int err = -ENOBUFS;
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200582 struct net *net;
583 u16 port_no = 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700584
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200585 if (WARN_ON(!port && !br))
Vlad Yasevich407af322013-02-13 12:00:12 +0000586 return;
587
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200588 if (port) {
589 dev = port->dev;
590 br = port->br;
591 port_no = port->port_no;
592 } else {
593 dev = br->dev;
594 }
stephen hemminger28a16c92010-05-10 09:31:09 +0000595
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200596 net = dev_net(dev);
597 br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event);
598
599 skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC);
Thomas Graf280a3062006-08-15 00:36:28 -0700600 if (skb == NULL)
601 goto errout;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700602
Henrik Bjoernlundb6d04252020-10-27 10:02:51 +0000603 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev, false);
Patrick McHardy26932562007-01-31 23:16:40 -0800604 if (err < 0) {
605 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
606 WARN_ON(err == -EMSGSIZE);
607 kfree_skb(skb);
608 goto errout;
609 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -0800610 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
611 return;
Thomas Graf280a3062006-08-15 00:36:28 -0700612errout:
tanxiaojun87e823b2013-12-19 13:28:10 +0800613 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700614}
615
Henrik Bjoernlundb6d04252020-10-27 10:02:51 +0000616/* Notify listeners of a change in bridge or port information */
617void br_ifinfo_notify(int event, const struct net_bridge *br,
618 const struct net_bridge_port *port)
619{
620 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
621
622 return br_info_notify(event, br, port, filter);
623}
624
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700625/*
626 * Dump information about all ports, in response to GETLINK
627 */
John Fastabende5a55a82012-10-24 08:12:57 +0000628int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200629 struct net_device *dev, u32 filter_mask, int nlflags)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700630{
Hong Zhiguo1fb17542013-09-14 22:42:27 +0800631 struct net_bridge_port *port = br_port_get_rtnl(dev);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700632
Roopa Prabhu36cd0ff2015-01-10 07:31:14 -0800633 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
Horatiu Vultur36a8e8e2020-07-02 10:13:07 +0200634 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) &&
Henrik Bjoernlund5e312fc2020-10-27 10:02:49 +0000635 !(filter_mask & RTEXT_FILTER_MRP) &&
Henrik Bjoernlunde77824d2020-10-27 10:02:50 +0000636 !(filter_mask & RTEXT_FILTER_CFM_CONFIG) &&
637 !(filter_mask & RTEXT_FILTER_CFM_STATUS))
Dan Carpenter1b846f92015-01-21 12:22:35 +0300638 return 0;
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000639
Nicolas Dichtel46c264d2015-04-28 18:33:49 +0200640 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
Henrik Bjoernlundb6d04252020-10-27 10:02:51 +0000641 filter_mask, dev, true);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700642}
643
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800644static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
Petr Machata169327d2018-12-12 17:02:50 +0000645 int cmd, struct bridge_vlan_info *vinfo, bool *changed,
646 struct netlink_ext_ack *extack)
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800647{
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300648 bool curr_change;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800649 int err = 0;
650
651 switch (cmd) {
652 case RTM_SETLINK:
653 if (p) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200654 /* if the MASTER flag is set this will act on the global
655 * per-VLAN entry as well
656 */
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300657 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
Petr Machata169327d2018-12-12 17:02:50 +0000658 &curr_change, extack);
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800659 } else {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200660 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300661 err = br_vlan_add(br, vinfo->vid, vinfo->flags,
Petr Machata169327d2018-12-12 17:02:50 +0000662 &curr_change, extack);
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800663 }
Nikolay Aleksandrovf418af62017-10-27 13:19:37 +0300664 if (curr_change)
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300665 *changed = true;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800666 break;
667
668 case RTM_DELLINK:
669 if (p) {
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300670 if (!nbp_vlan_delete(p, vinfo->vid))
671 *changed = true;
672
673 if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
674 !br_vlan_delete(p->br, vinfo->vid))
675 *changed = true;
676 } else if (!br_vlan_delete(br, vinfo->vid)) {
677 *changed = true;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800678 }
679 break;
680 }
681
682 return err;
683}
Vlad Yasevich407af322013-02-13 12:00:12 +0000684
Nikolay Aleksandrovf26b2962020-01-14 19:56:10 +0200685int br_process_vlan_info(struct net_bridge *br,
686 struct net_bridge_port *p, int cmd,
687 struct bridge_vlan_info *vinfo_curr,
688 struct bridge_vlan_info **vinfo_last,
689 bool *changed,
690 struct netlink_ext_ack *extack)
Roopa Prabhuefa53562017-01-31 22:59:54 -0800691{
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200692 int err, rtm_cmd;
693
Nikolay Aleksandrov8f4cc942020-01-14 19:56:08 +0200694 if (!br_vlan_valid_id(vinfo_curr->vid, extack))
Roopa Prabhuefa53562017-01-31 22:59:54 -0800695 return -EINVAL;
696
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200697 /* needed for vlan-only NEWVLAN/DELVLAN notifications */
698 rtm_cmd = br_afspec_cmd_to_rtm(cmd);
699
Roopa Prabhuefa53562017-01-31 22:59:54 -0800700 if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
Nikolay Aleksandrov8f4cc942020-01-14 19:56:08 +0200701 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
Roopa Prabhuefa53562017-01-31 22:59:54 -0800702 return -EINVAL;
703 *vinfo_last = vinfo_curr;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800704 return 0;
705 }
706
707 if (*vinfo_last) {
708 struct bridge_vlan_info tmp_vinfo;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200709 int v, v_change_start = 0;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800710
Nikolay Aleksandrov8f4cc942020-01-14 19:56:08 +0200711 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
Roopa Prabhuefa53562017-01-31 22:59:54 -0800712 return -EINVAL;
713
714 memcpy(&tmp_vinfo, *vinfo_last,
715 sizeof(struct bridge_vlan_info));
716 for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200717 bool curr_change = false;
718
Roopa Prabhuefa53562017-01-31 22:59:54 -0800719 tmp_vinfo.vid = v;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200720 err = br_vlan_info(br, p, cmd, &tmp_vinfo, &curr_change,
Petr Machata169327d2018-12-12 17:02:50 +0000721 extack);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800722 if (err)
723 break;
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200724 if (curr_change) {
725 *changed = curr_change;
726 if (!v_change_start)
727 v_change_start = v;
728 } else {
729 /* nothing to notify yet */
730 if (!v_change_start)
731 continue;
732 br_vlan_notify(br, p, v_change_start,
733 v - 1, rtm_cmd);
734 v_change_start = 0;
735 }
Ido Schimmel79794572020-04-30 22:38:45 +0300736 cond_resched();
Roopa Prabhuefa53562017-01-31 22:59:54 -0800737 }
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200738 /* v_change_start is set only if the last/whole range changed */
739 if (v_change_start)
740 br_vlan_notify(br, p, v_change_start,
741 v - 1, rtm_cmd);
742
Roopa Prabhuefa53562017-01-31 22:59:54 -0800743 *vinfo_last = NULL;
744
Nikolay Aleksandrov66c54512017-10-19 20:17:32 +0300745 return err;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800746 }
747
Nikolay Aleksandrovf5459232020-01-14 19:56:14 +0200748 err = br_vlan_info(br, p, cmd, vinfo_curr, changed, extack);
749 if (*changed)
750 br_vlan_notify(br, p, vinfo_curr->vid, 0, rtm_cmd);
751
752 return err;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800753}
754
Vlad Yasevich407af322013-02-13 12:00:12 +0000755static int br_afspec(struct net_bridge *br,
756 struct net_bridge_port *p,
757 struct nlattr *af_spec,
Petr Machata169327d2018-12-12 17:02:50 +0000758 int cmd, bool *changed,
759 struct netlink_ext_ack *extack)
Vlad Yasevich407af322013-02-13 12:00:12 +0000760{
Roopa Prabhuefa53562017-01-31 22:59:54 -0800761 struct bridge_vlan_info *vinfo_curr = NULL;
762 struct bridge_vlan_info *vinfo_last = NULL;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800763 struct nlattr *attr;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800764 struct vtunnel_info tinfo_last = {};
765 struct vtunnel_info tinfo_curr = {};
766 int err = 0, rem;
Vlad Yasevich407af322013-02-13 12:00:12 +0000767
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800768 nla_for_each_nested(attr, af_spec, rem) {
Roopa Prabhuefa53562017-01-31 22:59:54 -0800769 err = 0;
770 switch (nla_type(attr)) {
771 case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
Nikolay Aleksandrov1020ce32017-06-06 01:26:24 +0300772 if (!p || !(p->flags & BR_VLAN_TUNNEL))
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800773 return -EINVAL;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800774 err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
775 if (err)
776 return err;
777 err = br_process_vlan_tunnel_info(br, p, cmd,
778 &tinfo_curr,
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300779 &tinfo_last,
780 changed);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800781 if (err)
782 return err;
Roopa Prabhubdced7e2015-01-10 07:31:12 -0800783 break;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800784 case IFLA_BRIDGE_VLAN_INFO:
785 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
786 return -EINVAL;
787 vinfo_curr = nla_data(attr);
788 err = br_process_vlan_info(br, p, cmd, vinfo_curr,
Petr Machata169327d2018-12-12 17:02:50 +0000789 &vinfo_last, changed,
790 extack);
Roopa Prabhuefa53562017-01-31 22:59:54 -0800791 if (err)
792 return err;
793 break;
Horatiu Vultur65369932020-04-26 15:22:07 +0200794 case IFLA_BRIDGE_MRP:
795 err = br_mrp_parse(br, p, attr, cmd, extack);
796 if (err)
797 return err;
798 break;
Henrik Bjoernlund2be665c2020-10-27 10:02:48 +0000799 case IFLA_BRIDGE_CFM:
800 err = br_cfm_parse(br, p, attr, cmd, extack);
801 if (err)
802 return err;
803 break;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800804 }
Vlad Yasevich407af322013-02-13 12:00:12 +0000805 }
806
807 return err;
808}
809
Jiri Pirko3ac636b2014-09-05 15:51:30 +0200810static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
stephen hemminger25c71c72012-11-13 07:53:05 +0000811 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
812 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
813 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
814 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
stephen hemmingera2e01a62012-11-13 07:53:07 +0000815 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
stephen hemminger1007dd12012-11-13 07:53:08 +0000816 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
Thomas Graf6f705d82014-11-26 13:42:19 +0100817 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
Vlad Yasevich9ba18892013-06-05 10:08:00 -0400818 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
Vlad Yasevich867a5942013-06-05 10:08:01 -0400819 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrov355b9f92015-08-04 19:06:32 +0200820 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
Nikolay Aleksandrov786c2072015-08-04 19:06:33 +0200821 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200822 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100823 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
Tobias Klauser90512472017-05-05 16:36:53 +0200824 [IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
825 [IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
Nikolay Aleksandrovfbec4432017-11-13 18:30:55 +0200826 [IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300827 [IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov1a3fbd32017-10-30 12:56:33 +0200828 [IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
Nikolay Aleksandrov7d850ab2018-05-24 11:56:48 +0300829 [IFLA_BRPORT_ISOLATED] = { .type = NLA_U8 },
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300830 [IFLA_BRPORT_BACKUP_PORT] = { .type = NLA_U32 },
Nikolay Aleksandrov2dba4072021-01-26 11:35:33 +0200831 [IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT] = { .type = NLA_U32 },
stephen hemminger25c71c72012-11-13 07:53:05 +0000832};
833
834/* Change the state of the port and notify spanning tree */
835static int br_set_port_state(struct net_bridge_port *p, u8 state)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700836{
stephen hemminger25c71c72012-11-13 07:53:05 +0000837 if (state > BR_STATE_BLOCKING)
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000838 return -EINVAL;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700839
840 /* if kernel STP is running, don't allow changes */
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700841 if (p->br->stp_enabled == BR_KERNEL_STP)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700842 return -EBUSY;
843
stephen hemminger576eb622012-12-28 18:15:22 +0000844 /* if device is not up, change is not allowed
845 * if link is not present, only allowable state is disabled
846 */
stephen hemminger25c71c72012-11-13 07:53:05 +0000847 if (!netif_running(p->dev) ||
stephen hemminger576eb622012-12-28 18:15:22 +0000848 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700849 return -ENETDOWN;
850
Florian Fainelli775dd692014-09-30 16:13:19 -0700851 br_set_state(p, state);
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000852 br_port_state_selection(p->br);
Stephen Hemminger11dc1f32006-05-25 16:00:12 -0700853 return 0;
854}
855
stephen hemminger25c71c72012-11-13 07:53:05 +0000856/* Set/clear or port flags based on attribute */
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200857static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
858 int attrtype, unsigned long mask)
stephen hemminger25c71c72012-11-13 07:53:05 +0000859{
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200860 if (!tb[attrtype])
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200861 return;
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200862
863 if (nla_get_u8(tb[attrtype]))
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200864 p->flags |= mask;
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200865 else
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200866 p->flags &= ~mask;
stephen hemminger25c71c72012-11-13 07:53:05 +0000867}
868
869/* Process bridge protocol info on port */
Vladimir Oltean078bbb82021-02-12 17:15:53 +0200870static int br_setport(struct net_bridge_port *p, struct nlattr *tb[],
871 struct netlink_ext_ack *extack)
stephen hemminger25c71c72012-11-13 07:53:05 +0000872{
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200873 unsigned long old_flags, changed_mask;
874 bool br_vlan_tunnel_old;
Roopa Prabhuefa53562017-01-31 22:59:54 -0800875 int err;
stephen hemminger25c71c72012-11-13 07:53:05 +0000876
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200877 old_flags = p->flags;
878 br_vlan_tunnel_old = (old_flags & BR_VLAN_TUNNEL) ? true : false;
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200879
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200880 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
881 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
882 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE,
883 BR_MULTICAST_FAST_LEAVE);
884 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
885 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
886 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
887 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
888 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST,
889 BR_MULTICAST_TO_UNICAST);
890 br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
891 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
892 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
893 br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
894 br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS, BR_NEIGH_SUPPRESS);
895 br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200896
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200897 changed_mask = old_flags ^ p->flags;
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200898
Vladimir Oltean078bbb82021-02-12 17:15:53 +0200899 err = br_switchdev_set_port_flag(p, p->flags, changed_mask, extack);
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200900 if (err) {
901 p->flags = old_flags;
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200902 return err;
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200903 }
Arkadi Sharshevsky39222852017-06-08 08:44:11 +0200904
Roopa Prabhuefa53562017-01-31 22:59:54 -0800905 if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
906 nbp_vlan_tunnel_info_flush(p);
907
Vladimir Oltean304ae3b2021-02-12 17:15:52 +0200908 br_port_flags_change(p, changed_mask);
909
stephen hemminger25c71c72012-11-13 07:53:05 +0000910 if (tb[IFLA_BRPORT_COST]) {
911 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
912 if (err)
913 return err;
914 }
915
916 if (tb[IFLA_BRPORT_PRIORITY]) {
917 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
918 if (err)
919 return err;
920 }
921
922 if (tb[IFLA_BRPORT_STATE]) {
923 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
924 if (err)
925 return err;
926 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400927
Nikolay Aleksandrov9b0c6e42015-10-06 14:12:01 +0200928 if (tb[IFLA_BRPORT_FLUSH])
929 br_fdb_delete_by_port(p->br, p, 0, 0);
930
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200931#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
932 if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
933 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
934
Nikolay Aleksandrova53581d2021-08-20 15:42:54 +0300935 err = br_multicast_set_port_router(&p->multicast_ctx,
936 mcast_router);
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200937 if (err)
938 return err;
939 }
Nikolay Aleksandrov2dba4072021-01-26 11:35:33 +0200940
941 if (tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]) {
942 u32 hlimit;
943
944 hlimit = nla_get_u32(tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]);
945 err = br_multicast_eht_set_hosts_limit(p, hlimit);
946 if (err)
947 return err;
948 }
Nikolay Aleksandrov5d6ae472015-10-06 14:12:02 +0200949#endif
Nikolay Aleksandrov5af48b52017-09-27 16:12:44 +0300950
951 if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
952 u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
953
954 if (fwd_mask & BR_GROUPFWD_MACPAUSE)
955 return -EINVAL;
956 p->group_fwd_mask = fwd_mask;
957 }
958
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300959 if (tb[IFLA_BRPORT_BACKUP_PORT]) {
960 struct net_device *backup_dev = NULL;
961 u32 backup_ifindex;
962
963 backup_ifindex = nla_get_u32(tb[IFLA_BRPORT_BACKUP_PORT]);
964 if (backup_ifindex) {
965 backup_dev = __dev_get_by_index(dev_net(p->dev),
966 backup_ifindex);
967 if (!backup_dev)
968 return -ENOENT;
969 }
970
971 err = nbp_backup_change(p, backup_dev);
972 if (err)
973 return err;
974 }
975
stephen hemminger25c71c72012-11-13 07:53:05 +0000976 return 0;
977}
978
979/* Change state and parameters on port. */
Petr Machata2fd527b2018-12-12 17:02:48 +0000980int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags,
981 struct netlink_ext_ack *extack)
stephen hemminger25c71c72012-11-13 07:53:05 +0000982{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200983 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
984 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
985 struct net_bridge_port *p;
stephen hemminger25c71c72012-11-13 07:53:05 +0000986 struct nlattr *protinfo;
Vlad Yasevich407af322013-02-13 12:00:12 +0000987 struct nlattr *afspec;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +0300988 bool changed = false;
Scott Feldman41c498b2015-05-10 09:47:59 -0700989 int err = 0;
stephen hemminger25c71c72012-11-13 07:53:05 +0000990
Hong zhi guoc60ee672013-03-28 06:21:22 +0000991 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
992 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +0000993 if (!protinfo && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +0000994 return 0;
995
996 p = br_port_get_rtnl(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +0000997 /* We want to accept dev as bridge itself if the AF_SPEC
stephen hemminger8e3bff92013-12-08 12:15:44 -0800998 * is set to see if someone is setting vlan info on the bridge
Vlad Yasevich407af322013-02-13 12:00:12 +0000999 */
Hong zhi guo7b99a992013-03-24 03:26:47 +00001000 if (!p && !afspec)
stephen hemminger25c71c72012-11-13 07:53:05 +00001001 return -EINVAL;
1002
Vlad Yasevich407af322013-02-13 12:00:12 +00001003 if (p && protinfo) {
1004 if (protinfo->nla_type & NLA_F_NESTED) {
Johannes Berg8cb08172019-04-26 14:07:28 +02001005 err = nla_parse_nested_deprecated(tb, IFLA_BRPORT_MAX,
1006 protinfo,
1007 br_port_policy,
1008 NULL);
Vlad Yasevich407af322013-02-13 12:00:12 +00001009 if (err)
1010 return err;
1011
1012 spin_lock_bh(&p->br->lock);
Vladimir Oltean078bbb82021-02-12 17:15:53 +02001013 err = br_setport(p, tb, extack);
Vlad Yasevich407af322013-02-13 12:00:12 +00001014 spin_unlock_bh(&p->br->lock);
1015 } else {
stephen hemminger8e3bff92013-12-08 12:15:44 -08001016 /* Binary compatibility with old RSTP */
Vlad Yasevich407af322013-02-13 12:00:12 +00001017 if (nla_len(protinfo) < sizeof(u8))
1018 return -EINVAL;
1019
1020 spin_lock_bh(&p->br->lock);
1021 err = br_set_port_state(p, nla_get_u8(protinfo));
1022 spin_unlock_bh(&p->br->lock);
1023 }
stephen hemminger25c71c72012-11-13 07:53:05 +00001024 if (err)
Vlad Yasevich407af322013-02-13 12:00:12 +00001025 goto out;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +03001026 changed = true;
Vlad Yasevich407af322013-02-13 12:00:12 +00001027 }
stephen hemminger25c71c72012-11-13 07:53:05 +00001028
Nikolay Aleksandrov92899062017-11-01 12:18:13 +02001029 if (afspec)
Petr Machata169327d2018-12-12 17:02:50 +00001030 err = br_afspec(br, p, afspec, RTM_SETLINK, &changed, extack);
stephen hemminger25c71c72012-11-13 07:53:05 +00001031
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +03001032 if (changed)
Nikolay Aleksandrov92899062017-11-01 12:18:13 +02001033 br_ifinfo_notify(RTM_NEWLINK, br, p);
Vlad Yasevich407af322013-02-13 12:00:12 +00001034out:
stephen hemminger25c71c72012-11-13 07:53:05 +00001035 return err;
1036}
1037
Vlad Yasevich407af322013-02-13 12:00:12 +00001038/* Delete port information */
Roopa Prabhuadd511b2015-01-29 22:40:12 -08001039int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
Vlad Yasevich407af322013-02-13 12:00:12 +00001040{
Nikolay Aleksandrov92899062017-11-01 12:18:13 +02001041 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
Vlad Yasevich407af322013-02-13 12:00:12 +00001042 struct net_bridge_port *p;
Nikolay Aleksandrov92899062017-11-01 12:18:13 +02001043 struct nlattr *afspec;
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +03001044 bool changed = false;
Scott Feldman85080252015-05-10 09:48:03 -07001045 int err = 0;
Vlad Yasevich407af322013-02-13 12:00:12 +00001046
Hong zhi guoc60ee672013-03-28 06:21:22 +00001047 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
Vlad Yasevich407af322013-02-13 12:00:12 +00001048 if (!afspec)
1049 return 0;
1050
1051 p = br_port_get_rtnl(dev);
1052 /* We want to accept dev as bridge itself as well */
Kyungrok Chung254ec032021-10-16 20:21:36 +09001053 if (!p && !netif_is_bridge_master(dev))
Vlad Yasevich407af322013-02-13 12:00:12 +00001054 return -EINVAL;
1055
Petr Machata169327d2018-12-12 17:02:50 +00001056 err = br_afspec(br, p, afspec, RTM_DELLINK, &changed, NULL);
Nikolay Aleksandrove19b42a2017-10-27 13:19:36 +03001057 if (changed)
Roopa Prabhu02dba432015-01-14 20:02:25 -08001058 /* Send RTM_NEWLINK because userspace
1059 * expects RTM_NEWLINK for vlan dels
1060 */
Nikolay Aleksandrov92899062017-11-01 12:18:13 +02001061 br_ifinfo_notify(RTM_NEWLINK, br, p);
Vlad Yasevich407af322013-02-13 12:00:12 +00001062
1063 return err;
1064}
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001065
1066static int br_validate(struct nlattr *tb[], struct nlattr *data[],
1067 struct netlink_ext_ack *extack)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001068{
stephen hemmingerbb900b22011-04-04 14:03:32 +00001069 if (tb[IFLA_ADDRESS]) {
1070 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1071 return -EINVAL;
1072 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1073 return -EADDRNOTAVAIL;
1074 }
Thomas Graf32fe21c2007-03-22 11:59:03 -07001075
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001076 if (!data)
1077 return 0;
1078
1079#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Menglong Donga98c0c42021-01-17 16:09:50 +08001080 if (data[IFLA_BR_VLAN_PROTOCOL] &&
1081 !eth_type_vlan(nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])))
1082 return -EPROTONOSUPPORT;
Tobias Jungela2858602017-05-17 09:29:12 +02001083
1084 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1085 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1086
1087 if (defpvid >= VLAN_VID_MASK)
1088 return -EINVAL;
1089 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001090#endif
1091
Thomas Graf32fe21c2007-03-22 11:59:03 -07001092 return 0;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001093}
1094
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001095static int br_port_slave_changelink(struct net_device *brdev,
1096 struct net_device *dev,
1097 struct nlattr *tb[],
Matthias Schiffer17dd0ec2017-06-25 23:56:02 +02001098 struct nlattr *data[],
1099 struct netlink_ext_ack *extack)
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001100{
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +02001101 struct net_bridge *br = netdev_priv(brdev);
1102 int ret;
1103
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001104 if (!data)
1105 return 0;
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +02001106
1107 spin_lock_bh(&br->lock);
Vladimir Oltean078bbb82021-02-12 17:15:53 +02001108 ret = br_setport(br_port_get_rtnl(dev), data, extack);
Nikolay Aleksandrov963ad942015-07-22 13:03:40 +02001109 spin_unlock_bh(&br->lock);
1110
1111 return ret;
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001112}
1113
Jiri Pirkoced82832014-09-05 15:51:29 +02001114static int br_port_fill_slave_info(struct sk_buff *skb,
1115 const struct net_device *brdev,
1116 const struct net_device *dev)
1117{
1118 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
1119}
1120
1121static size_t br_port_get_slave_size(const struct net_device *brdev,
1122 const struct net_device *dev)
1123{
1124 return br_port_info_size();
1125}
1126
Jiri Pirko13323512014-09-05 15:51:32 +02001127static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
1128 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
1129 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
1130 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
Jörg Thalheimaf615762015-03-18 10:06:58 +01001131 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
1132 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
1133 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001134 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001135 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001136 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001137 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
1138 .len = ETH_ALEN },
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001139 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001140 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001141 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001142 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001143 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001144 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001145 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001146 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001147 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1148 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1149 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1150 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1151 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1152 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001153 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1154 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1155 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001156 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001157 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001158 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001159 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001160 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001161 [IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
Johannes Berg81408602020-08-18 10:17:31 +02001162 [IFLA_BR_MULTI_BOOLOPT] =
1163 NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)),
Jiri Pirko13323512014-09-05 15:51:32 +02001164};
1165
1166static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
Matthias Schifferad744b22017-06-25 23:56:00 +02001167 struct nlattr *data[],
1168 struct netlink_ext_ack *extack)
Jiri Pirko13323512014-09-05 15:51:32 +02001169{
1170 struct net_bridge *br = netdev_priv(brdev);
1171 int err;
1172
1173 if (!data)
1174 return 0;
1175
1176 if (data[IFLA_BR_FORWARD_DELAY]) {
1177 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1178 if (err)
1179 return err;
1180 }
1181
1182 if (data[IFLA_BR_HELLO_TIME]) {
1183 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1184 if (err)
1185 return err;
1186 }
1187
1188 if (data[IFLA_BR_MAX_AGE]) {
1189 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1190 if (err)
1191 return err;
1192 }
1193
Jörg Thalheimaf615762015-03-18 10:06:58 +01001194 if (data[IFLA_BR_AGEING_TIME]) {
Scott Feldmanc62987b2015-10-08 19:23:19 -07001195 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1196 if (err)
1197 return err;
Jörg Thalheimaf615762015-03-18 10:06:58 +01001198 }
1199
1200 if (data[IFLA_BR_STP_STATE]) {
1201 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1202
Horatiu Vultur419dba8a2020-04-26 15:22:08 +02001203 err = br_stp_set_enabled(br, stp_enabled, extack);
1204 if (err)
1205 return err;
Jörg Thalheimaf615762015-03-18 10:06:58 +01001206 }
1207
1208 if (data[IFLA_BR_PRIORITY]) {
1209 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1210
1211 br_stp_set_bridge_priority(br, priority);
1212 }
1213
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001214 if (data[IFLA_BR_VLAN_FILTERING]) {
1215 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1216
Vladimir Oltean9e781402021-02-13 22:43:16 +02001217 err = br_vlan_filter_toggle(br, vlan_filter, extack);
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001218 if (err)
1219 return err;
1220 }
1221
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001222#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1223 if (data[IFLA_BR_VLAN_PROTOCOL]) {
1224 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1225
Vladimir Olteandcbdf132021-02-13 22:43:17 +02001226 err = __br_vlan_set_proto(br, vlan_proto, extack);
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001227 if (err)
1228 return err;
1229 }
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001230
1231 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1232 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1233
Petr Machata169327d2018-12-12 17:02:50 +00001234 err = __br_vlan_set_default_pvid(br, defpvid, extack);
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001235 if (err)
1236 return err;
1237 }
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001238
1239 if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1240 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1241
1242 err = br_vlan_set_stats(br, vlan_stats);
1243 if (err)
1244 return err;
1245 }
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001246
1247 if (data[IFLA_BR_VLAN_STATS_PER_PORT]) {
1248 __u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]);
1249
1250 err = br_vlan_set_stats_per_port(br, per_port);
1251 if (err)
1252 return err;
1253 }
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001254#endif
1255
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001256 if (data[IFLA_BR_GROUP_FWD_MASK]) {
1257 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1258
1259 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1260 return -EINVAL;
1261 br->group_fwd_mask = fwd_mask;
1262 }
1263
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001264 if (data[IFLA_BR_GROUP_ADDR]) {
1265 u8 new_addr[ETH_ALEN];
1266
1267 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1268 return -EINVAL;
1269 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1270 if (!is_link_local_ether_addr(new_addr))
1271 return -EINVAL;
1272 if (new_addr[5] == 1 || /* 802.3x Pause address */
1273 new_addr[5] == 2 || /* 802.3ad Slow protocols */
1274 new_addr[5] == 3) /* 802.1X PAE address */
1275 return -EINVAL;
1276 spin_lock_bh(&br->lock);
1277 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1278 spin_unlock_bh(&br->lock);
Nikolay Aleksandrovbe3664a2018-09-26 17:01:02 +03001279 br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001280 br_recalculate_fwd_mask(br);
1281 }
1282
Nikolay Aleksandrov150217c2015-10-04 14:23:36 +02001283 if (data[IFLA_BR_FDB_FLUSH])
1284 br_fdb_flush(br);
1285
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001286#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1287 if (data[IFLA_BR_MCAST_ROUTER]) {
1288 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1289
Nikolay Aleksandrova97df082021-08-10 18:29:31 +03001290 err = br_multicast_set_router(&br->multicast_ctx,
1291 multicast_router);
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001292 if (err)
1293 return err;
1294 }
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001295
1296 if (data[IFLA_BR_MCAST_SNOOPING]) {
1297 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1298
Florian Fainelliae1ea842021-04-14 22:22:57 +03001299 err = br_multicast_toggle(br, mcast_snooping, extack);
1300 if (err)
1301 return err;
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001302 }
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001303
1304 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1305 u8 val;
1306
1307 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001308 br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001309 }
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001310
1311 if (data[IFLA_BR_MCAST_QUERIER]) {
1312 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1313
Nikolay Aleksandrov62938182021-08-10 18:29:30 +03001314 err = br_multicast_set_querier(&br->multicast_ctx,
1315 mcast_querier);
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001316 if (err)
1317 return err;
1318 }
Nikolay Aleksandrov431db3c2015-10-04 14:23:41 +02001319
Nikolay Aleksandrovcf332bca2018-12-05 15:14:26 +02001320 if (data[IFLA_BR_MCAST_HASH_ELASTICITY])
1321 br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
1322 RHT_ELASTICITY);
Nikolay Aleksandrov858079f2015-10-04 14:23:42 +02001323
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +02001324 if (data[IFLA_BR_MCAST_HASH_MAX])
1325 br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001326
1327 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1328 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1329
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001330 br->multicast_ctx.multicast_last_member_count = val;
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001331 }
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001332
1333 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1334 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1335
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001336 br->multicast_ctx.multicast_startup_query_count = val;
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001337 }
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001338
1339 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1340 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1341
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001342 br->multicast_ctx.multicast_last_member_interval = clock_t_to_jiffies(val);
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001343 }
1344
1345 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1346 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1347
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001348 br->multicast_ctx.multicast_membership_interval = clock_t_to_jiffies(val);
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001349 }
1350
1351 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1352 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1353
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001354 br->multicast_ctx.multicast_querier_interval = clock_t_to_jiffies(val);
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001355 }
1356
1357 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1358 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1359
Nikolay Aleksandrov99b40612021-12-27 19:21:15 +02001360 br_multicast_set_query_intvl(&br->multicast_ctx, val);
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001361 }
1362
1363 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1364 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1365
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001366 br->multicast_ctx.multicast_query_response_interval = clock_t_to_jiffies(val);
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001367 }
1368
1369 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1370 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1371
Nikolay Aleksandrovf83a1122021-12-27 19:21:16 +02001372 br_multicast_set_startup_query_intvl(&br->multicast_ctx, val);
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001373 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001374
1375 if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1376 __u8 mcast_stats;
1377
1378 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001379 br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001380 }
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001381
1382 if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1383 __u8 igmp_version;
1384
1385 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
Nikolay Aleksandrovdf271cd2021-08-10 18:29:19 +03001386 err = br_multicast_set_igmp_version(&br->multicast_ctx,
1387 igmp_version);
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001388 if (err)
1389 return err;
1390 }
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001391
1392#if IS_ENABLED(CONFIG_IPV6)
1393 if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1394 __u8 mld_version;
1395
1396 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
Nikolay Aleksandrovdf271cd2021-08-10 18:29:19 +03001397 err = br_multicast_set_mld_version(&br->multicast_ctx,
1398 mld_version);
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001399 if (err)
1400 return err;
1401 }
1402#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001403#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001404#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1405 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1406 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1407
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001408 br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001409 }
1410
1411 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1412 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1413
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001414 br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001415 }
1416
1417 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1418 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1419
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001420 br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001421 }
1422#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001423
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001424 if (data[IFLA_BR_MULTI_BOOLOPT]) {
1425 struct br_boolopt_multi *bm;
1426
1427 bm = nla_data(data[IFLA_BR_MULTI_BOOLOPT]);
1428 err = br_boolopt_multi_toggle(br, bm, extack);
1429 if (err)
1430 return err;
1431 }
1432
Jiri Pirko13323512014-09-05 15:51:32 +02001433 return 0;
1434}
1435
Ivan Vecerab6677442017-01-20 18:12:17 +01001436static int br_dev_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +02001437 struct nlattr *tb[], struct nlattr *data[],
1438 struct netlink_ext_ack *extack)
Ivan Vecerab6677442017-01-20 18:12:17 +01001439{
1440 struct net_bridge *br = netdev_priv(dev);
1441 int err;
1442
Nikolay Aleksandrov84aeb432017-12-18 17:35:09 +02001443 err = register_netdevice(dev);
1444 if (err)
1445 return err;
1446
Ivan Vecerab6677442017-01-20 18:12:17 +01001447 if (tb[IFLA_ADDRESS]) {
1448 spin_lock_bh(&br->lock);
1449 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1450 spin_unlock_bh(&br->lock);
1451 }
1452
Matthias Schifferad744b22017-06-25 23:56:00 +02001453 err = br_changelink(dev, tb, data, extack);
Ido Schimmel5b8d5422017-04-10 14:59:28 +03001454 if (err)
Nikolay Aleksandrov84aeb432017-12-18 17:35:09 +02001455 br_dev_delete(dev, NULL);
1456
Ido Schimmel5b8d5422017-04-10 14:59:28 +03001457 return err;
Ivan Vecerab6677442017-01-20 18:12:17 +01001458}
1459
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001460static size_t br_get_size(const struct net_device *brdev)
1461{
1462 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1463 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1464 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
Jörg Thalheimaf615762015-03-18 10:06:58 +01001465 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1466 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1467 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001468 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001469#ifdef CONFIG_BRIDGE_VLAN_FILTERING
1470 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001471 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001472 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001473 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_PER_PORT */
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001474#endif
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001475 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001476 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
Nikolay Aleksandrov7599a222015-10-04 14:23:30 +02001477 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
Nikolay Aleksandrov8762ba62015-10-04 14:23:31 +02001478 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001479 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001480 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1481 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001482 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1483 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1484 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1485 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
Nikolay Aleksandrov111189a2015-10-04 14:23:35 +02001486 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001487#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1488 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
Nikolay Aleksandrov89126322015-10-04 14:23:38 +02001489 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001490 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
Nikolay Aleksandrovba062d7c2015-10-04 14:23:40 +02001491 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001492 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001493 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1494 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1495 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1496 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001497 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1498 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1499 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1500 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1501 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1502 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001503 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001504 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
Nikolay Aleksandrovc7fa1d92021-08-13 18:00:00 +03001505 br_multicast_querier_state_size() + /* IFLA_BR_MCAST_QUERIER_STATE */
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001506#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001507#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1508 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1509 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1510 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1511#endif
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001512 nla_total_size(sizeof(struct br_boolopt_multi)) + /* IFLA_BR_MULTI_BOOLOPT */
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001513 0;
1514}
1515
1516static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1517{
1518 struct net_bridge *br = netdev_priv(brdev);
1519 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1520 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1521 u32 age_time = jiffies_to_clock_t(br->max_age);
Jörg Thalheimaf615762015-03-18 10:06:58 +01001522 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1523 u32 stp_enabled = br->stp_enabled;
1524 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
Ido Schimmel1f514452017-05-26 08:37:23 +02001525 u8 vlan_enabled = br_vlan_enabled(br->dev);
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001526 struct br_boolopt_multi bm;
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001527 u64 clockval;
Nikolay Aleksandrov5127c812015-10-04 14:23:29 +02001528
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001529 clockval = br_timer_value(&br->hello_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001530 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001531 return -EMSGSIZE;
1532 clockval = br_timer_value(&br->tcn_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001533 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001534 return -EMSGSIZE;
1535 clockval = br_timer_value(&br->topology_change_timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001536 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1537 IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001538 return -EMSGSIZE;
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +01001539 clockval = br_timer_value(&br->gc_work.timer);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001540 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001541 return -EMSGSIZE;
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001542
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001543 br_boolopt_multi_get(br, &bm);
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001544 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1545 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
Jörg Thalheimaf615762015-03-18 10:06:58 +01001546 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1547 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1548 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
Nikolay Aleksandrova7854032015-08-07 19:40:45 +03001549 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
Nikolay Aleksandrov79102282015-10-04 14:23:28 +02001550 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
Nikolay Aleksandrov4917a152015-10-05 12:11:21 +02001551 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1552 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1553 &br->bridge_id) ||
1554 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1555 &br->designated_root) ||
Nikolay Aleksandrov684dd242015-10-04 14:23:32 +02001556 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
Nikolay Aleksandroved416302015-10-04 14:23:33 +02001557 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1558 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1559 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
Nikolay Aleksandrovd76bd142015-10-04 14:23:34 +02001560 br->topology_change_detected) ||
Nikolay Aleksandrova428afe2018-11-24 04:34:20 +02001561 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) ||
1562 nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm))
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001563 return -EMSGSIZE;
1564
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001565#ifdef CONFIG_BRIDGE_VLAN_FILTERING
Nikolay Aleksandrov0f963b72015-10-04 14:23:47 +02001566 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
Nikolay Aleksandrov6dada9b2016-04-30 10:25:28 +02001567 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
Nikolay Aleksandrovae757672018-09-26 17:01:00 +03001568 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED,
Nikolay Aleksandrov9163a0f2018-10-12 13:41:16 +03001569 br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) ||
1570 nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT,
Nikolay Aleksandrov600bea72019-04-16 16:15:56 +03001571 br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)))
Toshiaki Makitad2d427b2015-08-27 15:32:26 +09001572 return -EMSGSIZE;
1573#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001574#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001575 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER,
1576 br->multicast_ctx.multicast_router) ||
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +03001577 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING,
1578 br_opt_get(br, BROPT_MULTICAST_ENABLED)) ||
Nikolay Aleksandrov295141d2015-10-04 14:23:39 +02001579 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001580 br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) ||
1581 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER,
Nikolay Aleksandrov62938182021-08-10 18:29:30 +03001582 br->multicast_ctx.multicast_querier) ||
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001583 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
Nikolay Aleksandrov675779a2018-09-26 17:01:04 +03001584 br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
Nikolay Aleksandrovcf332bca2018-12-05 15:14:26 +02001585 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) ||
Nikolay Aleksandrov79b859f2015-10-04 14:23:43 +02001586 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1587 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001588 br->multicast_ctx.multicast_last_member_count) ||
Nikolay Aleksandrovb89e6ba2015-10-04 14:23:44 +02001589 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001590 br->multicast_ctx.multicast_startup_query_count) ||
Nikolay Aleksandrov5e923582016-11-21 13:03:24 +01001591 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
Nikolay Aleksandrovc7fa1d92021-08-13 18:00:00 +03001592 br->multicast_ctx.multicast_igmp_version) ||
1593 br_multicast_dump_querier_state(skb, &br->multicast_ctx,
1594 IFLA_BR_MCAST_QUERIER_STATE))
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001595 return -EMSGSIZE;
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001596#if IS_ENABLED(CONFIG_IPV6)
1597 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001598 br->multicast_ctx.multicast_mld_version))
Nikolay Aleksandrovaa2ae3e2016-11-21 13:03:25 +01001599 return -EMSGSIZE;
1600#endif
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001601 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_last_member_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001602 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1603 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001604 return -EMSGSIZE;
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001605 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_membership_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001606 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1607 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001608 return -EMSGSIZE;
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001609 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_querier_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001610 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1611 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001612 return -EMSGSIZE;
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001613 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001614 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1615 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001616 return -EMSGSIZE;
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001617 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_response_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001618 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1619 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001620 return -EMSGSIZE;
Nikolay Aleksandrovd3d065c2021-07-19 20:06:24 +03001621 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_startup_query_interval);
Nicolas Dichtel12a0faa2016-04-25 10:25:18 +02001622 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1623 IFLA_BR_PAD))
Nikolay Aleksandrov7e4df512015-10-04 14:23:45 +02001624 return -EMSGSIZE;
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001625#endif
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001626#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1627 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001628 br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) ||
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001629 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001630 br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) ||
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001631 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
Nikolay Aleksandrov8df35102018-09-26 17:01:01 +03001632 br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0))
Nikolay Aleksandrov93870cc2015-10-04 14:23:46 +02001633 return -EMSGSIZE;
1634#endif
Nikolay Aleksandrova9a6bc72015-10-04 14:23:37 +02001635
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001636 return 0;
1637}
1638
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001639static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001640{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001641 struct net_bridge_port *p = NULL;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001642 struct net_bridge_vlan_group *vg;
1643 struct net_bridge_vlan *v;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001644 struct net_bridge *br;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001645 int numvls = 0;
1646
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001647 switch (attr) {
1648 case IFLA_STATS_LINK_XSTATS:
1649 br = netdev_priv(dev);
1650 vg = br_vlan_group(br);
1651 break;
1652 case IFLA_STATS_LINK_XSTATS_SLAVE:
1653 p = br_port_get_rtnl(dev);
1654 if (!p)
1655 return 0;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001656 vg = nbp_vlan_group(p);
1657 break;
1658 default:
1659 return 0;
1660 }
1661
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001662 if (vg) {
1663 /* we need to count all, even placeholder entries */
1664 list_for_each_entry(v, &vg->vlan_list, vlist)
1665 numvls++;
1666 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001667
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001668 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
Eric Dumazetdbe0b882021-10-04 18:05:07 -07001669 nla_total_size_64bit(sizeof(struct br_mcast_stats)) +
Eric Dumazet0854a0512021-10-04 18:05:08 -07001670 (p ? nla_total_size_64bit(sizeof(p->stp_xstats)) : 0) +
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001671 nla_total_size(0);
1672}
1673
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001674static int br_fill_linkxstats(struct sk_buff *skb,
1675 const struct net_device *dev,
1676 int *prividx, int attr)
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001677{
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001678 struct nlattr *nla __maybe_unused;
1679 struct net_bridge_port *p = NULL;
1680 struct net_bridge_vlan_group *vg;
1681 struct net_bridge_vlan *v;
1682 struct net_bridge *br;
1683 struct nlattr *nest;
1684 int vl_idx = 0;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001685
1686 switch (attr) {
1687 case IFLA_STATS_LINK_XSTATS:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001688 br = netdev_priv(dev);
1689 vg = br_vlan_group(br);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001690 break;
1691 case IFLA_STATS_LINK_XSTATS_SLAVE:
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001692 p = br_port_get_rtnl(dev);
1693 if (!p)
1694 return 0;
1695 br = p->br;
1696 vg = nbp_vlan_group(p);
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001697 break;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001698 default:
1699 return -EINVAL;
Nikolay Aleksandrov80e73cc2016-06-28 16:57:05 +02001700 }
1701
Michal Kubecekae0be8d2019-04-26 11:13:06 +02001702 nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE);
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001703 if (!nest)
1704 return -EMSGSIZE;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001705
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001706 if (vg) {
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001707 u16 pvid;
1708
1709 pvid = br_get_pvid(vg);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001710 list_for_each_entry(v, &vg->vlan_list, vlist) {
1711 struct bridge_vlan_xstats vxi;
Heiner Kallweit281cc282020-11-17 21:25:42 +01001712 struct pcpu_sw_netstats stats;
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001713
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001714 if (++vl_idx < *prividx)
1715 continue;
1716 memset(&vxi, 0, sizeof(vxi));
1717 vxi.vid = v->vid;
Nikolay Aleksandrov61ba1a22016-08-17 12:53:10 +02001718 vxi.flags = v->flags;
Nikolay Aleksandrov72f4af42016-08-25 14:27:51 +02001719 if (v->vid == pvid)
1720 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001721 br_vlan_get_stats(v, &stats);
1722 vxi.rx_bytes = stats.rx_bytes;
1723 vxi.rx_packets = stats.rx_packets;
1724 vxi.tx_bytes = stats.tx_bytes;
1725 vxi.tx_packets = stats.tx_packets;
1726
1727 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1728 goto nla_put_failure;
1729 }
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001730 }
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001731
1732#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1733 if (++vl_idx >= *prividx) {
1734 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1735 sizeof(struct br_mcast_stats),
1736 BRIDGE_XSTATS_PAD);
1737 if (!nla)
1738 goto nla_put_failure;
Nikolay Aleksandrovd5ff8c42016-08-17 12:53:09 +02001739 br_multicast_get_stats(br, p, nla_data(nla));
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001740 }
1741#endif
Vivien Didelotde179962019-12-11 20:07:10 -05001742
1743 if (p) {
1744 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_STP,
1745 sizeof(p->stp_xstats),
1746 BRIDGE_XSTATS_PAD);
1747 if (!nla)
1748 goto nla_put_failure;
1749
1750 spin_lock_bh(&br->lock);
1751 memcpy(nla_data(nla), &p->stp_xstats, sizeof(p->stp_xstats));
1752 spin_unlock_bh(&br->lock);
1753 }
1754
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001755 nla_nest_end(skb, nest);
1756 *prividx = 0;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +02001757
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001758 return 0;
1759
1760nla_put_failure:
1761 nla_nest_end(skb, nest);
1762 *prividx = vl_idx;
1763
1764 return -EMSGSIZE;
1765}
Roopa Prabhufed0a152015-02-25 23:55:40 -08001766
Daniel Borkmann207895fd32015-01-29 12:15:03 +01001767static struct rtnl_af_ops br_af_ops __read_mostly = {
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001768 .family = AF_BRIDGE,
Arad, Ronenb1974ed2015-10-19 09:23:28 -07001769 .get_link_af_size = br_get_link_af_size_filtered,
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001770};
1771
stephen hemminger149ddd82012-06-26 05:48:45 +00001772struct rtnl_link_ops br_link_ops __read_mostly = {
Jiri Pirkoced82832014-09-05 15:51:29 +02001773 .kind = "bridge",
1774 .priv_size = sizeof(struct net_bridge),
1775 .setup = br_dev_setup,
Scott Feldmaneb4cb852015-08-19 11:29:35 -07001776 .maxtype = IFLA_BR_MAX,
Jiri Pirko13323512014-09-05 15:51:32 +02001777 .policy = br_policy,
Jiri Pirkoced82832014-09-05 15:51:29 +02001778 .validate = br_validate,
1779 .newlink = br_dev_newlink,
Jiri Pirko13323512014-09-05 15:51:32 +02001780 .changelink = br_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001781 .dellink = br_dev_delete,
Jiri Pirkoe5c3ea52014-09-05 15:51:31 +02001782 .get_size = br_get_size,
1783 .fill_info = br_fill_info,
Nikolay Aleksandrova60c0902016-04-30 10:25:29 +02001784 .fill_linkxstats = br_fill_linkxstats,
1785 .get_linkxstats_size = br_get_linkxstats_size,
Jiri Pirko3ac636b2014-09-05 15:51:30 +02001786
1787 .slave_maxtype = IFLA_BRPORT_MAX,
1788 .slave_policy = br_port_policy,
1789 .slave_changelink = br_port_slave_changelink,
Jiri Pirkoced82832014-09-05 15:51:29 +02001790 .get_slave_size = br_port_get_slave_size,
1791 .fill_slave_info = br_port_fill_slave_info,
stephen hemmingerbb900b22011-04-04 14:03:32 +00001792};
1793
1794int __init br_netlink_init(void)
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001795{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001796 int err;
1797
1798 br_mdb_init();
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001799 br_vlan_rtnl_init();
stephen hemminger3678a9d2013-12-30 10:41:32 -08001800 rtnl_af_register(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001801
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001802 err = rtnl_link_register(&br_link_ops);
1803 if (err)
1804 goto out_af;
1805
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001806 return 0;
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001807
1808out_af:
1809 rtnl_af_unregister(&br_af_ops);
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001810 br_mdb_uninit();
1811 return err;
Stephen Hemminger11dc1f32006-05-25 16:00:12 -07001812}
1813
Pablo Neira Ayuso34666d42014-09-18 11:29:03 +02001814void br_netlink_fini(void)
stephen hemmingerbb900b22011-04-04 14:03:32 +00001815{
Vlad Yasevich3ec8e9f2013-01-02 09:41:25 +00001816 br_mdb_uninit();
Nikolay Aleksandrov8dcea182020-01-14 19:56:09 +02001817 br_vlan_rtnl_uninit();
Vlad Yasevich6cbdcee2013-02-13 12:00:13 +00001818 rtnl_af_unregister(&br_af_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001819 rtnl_link_unregister(&br_link_ops);
stephen hemmingerbb900b22011-04-04 14:03:32 +00001820}