1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020, Nikolay Aleksandrov <nikolay@cumulusnetworks.com>
3 #include <linux/kernel.h>
4 #include <linux/netdevice.h>
5 #include <linux/rtnetlink.h>
6 #include <linux/slab.h>
7 #include <net/ip_tunnels.h>
8 
9 #include "br_private.h"
10 #include "br_private_tunnel.h"
11 
__vlan_tun_put(struct sk_buff * skb,const struct net_bridge_vlan * v)12 static bool __vlan_tun_put(struct sk_buff *skb, const struct net_bridge_vlan *v)
13 {
14 	__be32 tid = tunnel_id_to_key32(v->tinfo.tunnel_id);
15 	struct nlattr *nest;
16 
17 	if (!v->tinfo.tunnel_dst)
18 		return true;
19 
20 	nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_TUNNEL_INFO);
21 	if (!nest)
22 		return false;
23 	if (nla_put_u32(skb, BRIDGE_VLANDB_TINFO_ID, be32_to_cpu(tid))) {
24 		nla_nest_cancel(skb, nest);
25 		return false;
26 	}
27 	nla_nest_end(skb, nest);
28 
29 	return true;
30 }
31 
__vlan_tun_can_enter_range(const struct net_bridge_vlan * v_curr,const struct net_bridge_vlan * range_end)32 static bool __vlan_tun_can_enter_range(const struct net_bridge_vlan *v_curr,
33 				       const struct net_bridge_vlan *range_end)
34 {
35 	return (!v_curr->tinfo.tunnel_dst && !range_end->tinfo.tunnel_dst) ||
36 	       vlan_tunid_inrange(v_curr, range_end);
37 }
38 
39 /* check if the options' state of v_curr allow it to enter the range */
br_vlan_opts_eq_range(const struct net_bridge_vlan * v_curr,const struct net_bridge_vlan * range_end)40 bool br_vlan_opts_eq_range(const struct net_bridge_vlan *v_curr,
41 			   const struct net_bridge_vlan *range_end)
42 {
43 	u8 range_mc_rtr = br_vlan_multicast_router(range_end);
44 	u8 curr_mc_rtr = br_vlan_multicast_router(v_curr);
45 
46 	return v_curr->state == range_end->state &&
47 	       __vlan_tun_can_enter_range(v_curr, range_end) &&
48 	       curr_mc_rtr == range_mc_rtr;
49 }
50 
br_vlan_opts_fill(struct sk_buff * skb,const struct net_bridge_vlan * v)51 bool br_vlan_opts_fill(struct sk_buff *skb, const struct net_bridge_vlan *v)
52 {
53 	if (nla_put_u8(skb, BRIDGE_VLANDB_ENTRY_STATE, br_vlan_get_state(v)) ||
54 	    !__vlan_tun_put(skb, v))
55 		return false;
56 
57 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
58 	if (nla_put_u8(skb, BRIDGE_VLANDB_ENTRY_MCAST_ROUTER,
59 		       br_vlan_multicast_router(v)))
60 		return false;
61 #endif
62 
63 	return true;
64 }
65 
br_vlan_opts_nl_size(void)66 size_t br_vlan_opts_nl_size(void)
67 {
68 	return nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_ENTRY_STATE */
69 	       + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY_TUNNEL_INFO */
70 	       + nla_total_size(sizeof(u32)) /* BRIDGE_VLANDB_TINFO_ID */
71 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
72 	       + nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_ENTRY_MCAST_ROUTER */
73 #endif
74 	       + 0;
75 }
76 
br_vlan_modify_state(struct net_bridge_vlan_group * vg,struct net_bridge_vlan * v,u8 state,bool * changed,struct netlink_ext_ack * extack)77 static int br_vlan_modify_state(struct net_bridge_vlan_group *vg,
78 				struct net_bridge_vlan *v,
79 				u8 state,
80 				bool *changed,
81 				struct netlink_ext_ack *extack)
82 {
83 	struct net_bridge *br;
84 
85 	ASSERT_RTNL();
86 
87 	if (state > BR_STATE_BLOCKING) {
88 		NL_SET_ERR_MSG_MOD(extack, "Invalid vlan state");
89 		return -EINVAL;
90 	}
91 
92 	if (br_vlan_is_brentry(v))
93 		br = v->br;
94 	else
95 		br = v->port->br;
96 
97 	if (br->stp_enabled == BR_KERNEL_STP) {
98 		NL_SET_ERR_MSG_MOD(extack, "Can't modify vlan state when using kernel STP");
99 		return -EBUSY;
100 	}
101 
102 	if (br_opt_get(br, BROPT_MST_ENABLED)) {
103 		NL_SET_ERR_MSG_MOD(extack, "Can't modify vlan state directly when MST is enabled");
104 		return -EBUSY;
105 	}
106 
107 	if (v->state == state)
108 		return 0;
109 
110 	if (v->vid == br_get_pvid(vg))
111 		br_vlan_set_pvid_state(vg, state);
112 
113 	br_vlan_set_state(v, state);
114 	*changed = true;
115 
116 	return 0;
117 }
118 
119 static const struct nla_policy br_vlandb_tinfo_pol[BRIDGE_VLANDB_TINFO_MAX + 1] = {
120 	[BRIDGE_VLANDB_TINFO_ID]	= { .type = NLA_U32 },
121 	[BRIDGE_VLANDB_TINFO_CMD]	= { .type = NLA_U32 },
122 };
123 
br_vlan_modify_tunnel(const struct net_bridge_port * p,struct net_bridge_vlan * v,struct nlattr ** tb,bool * changed,struct netlink_ext_ack * extack)124 static int br_vlan_modify_tunnel(const struct net_bridge_port *p,
125 				 struct net_bridge_vlan *v,
126 				 struct nlattr **tb,
127 				 bool *changed,
128 				 struct netlink_ext_ack *extack)
129 {
130 	struct nlattr *tun_tb[BRIDGE_VLANDB_TINFO_MAX + 1], *attr;
131 	struct bridge_vlan_info *vinfo;
132 	u32 tun_id = 0;
133 	int cmd, err;
134 
135 	if (!p) {
136 		NL_SET_ERR_MSG_MOD(extack, "Can't modify tunnel mapping of non-port vlans");
137 		return -EINVAL;
138 	}
139 	if (!(p->flags & BR_VLAN_TUNNEL)) {
140 		NL_SET_ERR_MSG_MOD(extack, "Port doesn't have tunnel flag set");
141 		return -EINVAL;
142 	}
143 
144 	attr = tb[BRIDGE_VLANDB_ENTRY_TUNNEL_INFO];
145 	err = nla_parse_nested(tun_tb, BRIDGE_VLANDB_TINFO_MAX, attr,
146 			       br_vlandb_tinfo_pol, extack);
147 	if (err)
148 		return err;
149 
150 	if (!tun_tb[BRIDGE_VLANDB_TINFO_CMD]) {
151 		NL_SET_ERR_MSG_MOD(extack, "Missing tunnel command attribute");
152 		return -ENOENT;
153 	}
154 	cmd = nla_get_u32(tun_tb[BRIDGE_VLANDB_TINFO_CMD]);
155 	switch (cmd) {
156 	case RTM_SETLINK:
157 		if (!tun_tb[BRIDGE_VLANDB_TINFO_ID]) {
158 			NL_SET_ERR_MSG_MOD(extack, "Missing tunnel id attribute");
159 			return -ENOENT;
160 		}
161 		/* when working on vlan ranges this is the starting tunnel id */
162 		tun_id = nla_get_u32(tun_tb[BRIDGE_VLANDB_TINFO_ID]);
163 		/* vlan info attr is guaranteed by br_vlan_rtm_process_one */
164 		vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
165 		/* tunnel ids are mapped to each vlan in increasing order,
166 		 * the starting vlan is in BRIDGE_VLANDB_ENTRY_INFO and v is the
167 		 * current vlan, so we compute: tun_id + v - vinfo->vid
168 		 */
169 		tun_id += v->vid - vinfo->vid;
170 		break;
171 	case RTM_DELLINK:
172 		break;
173 	default:
174 		NL_SET_ERR_MSG_MOD(extack, "Unsupported tunnel command");
175 		return -EINVAL;
176 	}
177 
178 	return br_vlan_tunnel_info(p, cmd, v->vid, tun_id, changed);
179 }
180 
br_vlan_process_one_opts(const struct net_bridge * br,const struct net_bridge_port * p,struct net_bridge_vlan_group * vg,struct net_bridge_vlan * v,struct nlattr ** tb,bool * changed,struct netlink_ext_ack * extack)181 static int br_vlan_process_one_opts(const struct net_bridge *br,
182 				    const struct net_bridge_port *p,
183 				    struct net_bridge_vlan_group *vg,
184 				    struct net_bridge_vlan *v,
185 				    struct nlattr **tb,
186 				    bool *changed,
187 				    struct netlink_ext_ack *extack)
188 {
189 	int err;
190 
191 	*changed = false;
192 	if (tb[BRIDGE_VLANDB_ENTRY_STATE]) {
193 		u8 state = nla_get_u8(tb[BRIDGE_VLANDB_ENTRY_STATE]);
194 
195 		err = br_vlan_modify_state(vg, v, state, changed, extack);
196 		if (err)
197 			return err;
198 	}
199 	if (tb[BRIDGE_VLANDB_ENTRY_TUNNEL_INFO]) {
200 		err = br_vlan_modify_tunnel(p, v, tb, changed, extack);
201 		if (err)
202 			return err;
203 	}
204 
205 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
206 	if (tb[BRIDGE_VLANDB_ENTRY_MCAST_ROUTER]) {
207 		u8 val;
208 
209 		val = nla_get_u8(tb[BRIDGE_VLANDB_ENTRY_MCAST_ROUTER]);
210 		err = br_multicast_set_vlan_router(v, val);
211 		if (err)
212 			return err;
213 		*changed = true;
214 	}
215 #endif
216 
217 	return 0;
218 }
219 
br_vlan_process_options(const struct net_bridge * br,const struct net_bridge_port * p,struct net_bridge_vlan * range_start,struct net_bridge_vlan * range_end,struct nlattr ** tb,struct netlink_ext_ack * extack)220 int br_vlan_process_options(const struct net_bridge *br,
221 			    const struct net_bridge_port *p,
222 			    struct net_bridge_vlan *range_start,
223 			    struct net_bridge_vlan *range_end,
224 			    struct nlattr **tb,
225 			    struct netlink_ext_ack *extack)
226 {
227 	struct net_bridge_vlan *v, *curr_start = NULL, *curr_end = NULL;
228 	struct net_bridge_vlan_group *vg;
229 	int vid, err = 0;
230 	u16 pvid;
231 
232 	if (p)
233 		vg = nbp_vlan_group(p);
234 	else
235 		vg = br_vlan_group(br);
236 
237 	if (!range_start || !br_vlan_should_use(range_start)) {
238 		NL_SET_ERR_MSG_MOD(extack, "Vlan range start doesn't exist, can't process options");
239 		return -ENOENT;
240 	}
241 	if (!range_end || !br_vlan_should_use(range_end)) {
242 		NL_SET_ERR_MSG_MOD(extack, "Vlan range end doesn't exist, can't process options");
243 		return -ENOENT;
244 	}
245 
246 	pvid = br_get_pvid(vg);
247 	for (vid = range_start->vid; vid <= range_end->vid; vid++) {
248 		bool changed = false;
249 
250 		v = br_vlan_find(vg, vid);
251 		if (!v || !br_vlan_should_use(v)) {
252 			NL_SET_ERR_MSG_MOD(extack, "Vlan in range doesn't exist, can't process options");
253 			err = -ENOENT;
254 			break;
255 		}
256 
257 		err = br_vlan_process_one_opts(br, p, vg, v, tb, &changed,
258 					       extack);
259 		if (err)
260 			break;
261 
262 		if (changed) {
263 			/* vlan options changed, check for range */
264 			if (!curr_start) {
265 				curr_start = v;
266 				curr_end = v;
267 				continue;
268 			}
269 
270 			if (v->vid == pvid ||
271 			    !br_vlan_can_enter_range(v, curr_end)) {
272 				br_vlan_notify(br, p, curr_start->vid,
273 					       curr_end->vid, RTM_NEWVLAN);
274 				curr_start = v;
275 			}
276 			curr_end = v;
277 		} else {
278 			/* nothing changed and nothing to notify yet */
279 			if (!curr_start)
280 				continue;
281 
282 			br_vlan_notify(br, p, curr_start->vid, curr_end->vid,
283 				       RTM_NEWVLAN);
284 			curr_start = NULL;
285 			curr_end = NULL;
286 		}
287 	}
288 	if (curr_start)
289 		br_vlan_notify(br, p, curr_start->vid, curr_end->vid,
290 			       RTM_NEWVLAN);
291 
292 	return err;
293 }
294 
br_vlan_global_opts_can_enter_range(const struct net_bridge_vlan * v_curr,const struct net_bridge_vlan * r_end)295 bool br_vlan_global_opts_can_enter_range(const struct net_bridge_vlan *v_curr,
296 					 const struct net_bridge_vlan *r_end)
297 {
298 	return v_curr->vid - r_end->vid == 1 &&
299 		v_curr->msti == r_end->msti &&
300 	       ((v_curr->priv_flags ^ r_end->priv_flags) &
301 		BR_VLFLAG_GLOBAL_MCAST_ENABLED) == 0 &&
302 		br_multicast_ctx_options_equal(&v_curr->br_mcast_ctx,
303 					       &r_end->br_mcast_ctx);
304 }
305 
br_vlan_global_opts_fill(struct sk_buff * skb,u16 vid,u16 vid_range,const struct net_bridge_vlan * v_opts)306 bool br_vlan_global_opts_fill(struct sk_buff *skb, u16 vid, u16 vid_range,
307 			      const struct net_bridge_vlan *v_opts)
308 {
309 	struct nlattr *nest2 __maybe_unused;
310 	u64 clockval __maybe_unused;
311 	struct nlattr *nest;
312 
313 	nest = nla_nest_start(skb, BRIDGE_VLANDB_GLOBAL_OPTIONS);
314 	if (!nest)
315 		return false;
316 
317 	if (nla_put_u16(skb, BRIDGE_VLANDB_GOPTS_ID, vid))
318 		goto out_err;
319 
320 	if (vid_range && vid < vid_range &&
321 	    nla_put_u16(skb, BRIDGE_VLANDB_GOPTS_RANGE, vid_range))
322 		goto out_err;
323 
324 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
325 	if (nla_put_u8(skb, BRIDGE_VLANDB_GOPTS_MCAST_SNOOPING,
326 		       !!(v_opts->priv_flags & BR_VLFLAG_GLOBAL_MCAST_ENABLED)) ||
327 	    nla_put_u8(skb, BRIDGE_VLANDB_GOPTS_MCAST_IGMP_VERSION,
328 		       v_opts->br_mcast_ctx.multicast_igmp_version) ||
329 	    nla_put_u32(skb, BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_CNT,
330 			v_opts->br_mcast_ctx.multicast_last_member_count) ||
331 	    nla_put_u32(skb, BRIDGE_VLANDB_GOPTS_MCAST_STARTUP_QUERY_CNT,
332 			v_opts->br_mcast_ctx.multicast_startup_query_count) ||
333 	    nla_put_u8(skb, BRIDGE_VLANDB_GOPTS_MCAST_QUERIER,
334 		       v_opts->br_mcast_ctx.multicast_querier) ||
335 	    br_multicast_dump_querier_state(skb, &v_opts->br_mcast_ctx,
336 					    BRIDGE_VLANDB_GOPTS_MCAST_QUERIER_STATE))
337 		goto out_err;
338 
339 	clockval = jiffies_to_clock_t(v_opts->br_mcast_ctx.multicast_last_member_interval);
340 	if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_INTVL,
341 			      clockval, BRIDGE_VLANDB_GOPTS_PAD))
342 		goto out_err;
343 	clockval = jiffies_to_clock_t(v_opts->br_mcast_ctx.multicast_membership_interval);
344 	if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_GOPTS_MCAST_MEMBERSHIP_INTVL,
345 			      clockval, BRIDGE_VLANDB_GOPTS_PAD))
346 		goto out_err;
347 	clockval = jiffies_to_clock_t(v_opts->br_mcast_ctx.multicast_querier_interval);
348 	if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_GOPTS_MCAST_QUERIER_INTVL,
349 			      clockval, BRIDGE_VLANDB_GOPTS_PAD))
350 		goto out_err;
351 	clockval = jiffies_to_clock_t(v_opts->br_mcast_ctx.multicast_query_interval);
352 	if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_GOPTS_MCAST_QUERY_INTVL,
353 			      clockval, BRIDGE_VLANDB_GOPTS_PAD))
354 		goto out_err;
355 	clockval = jiffies_to_clock_t(v_opts->br_mcast_ctx.multicast_query_response_interval);
356 	if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_GOPTS_MCAST_QUERY_RESPONSE_INTVL,
357 			      clockval, BRIDGE_VLANDB_GOPTS_PAD))
358 		goto out_err;
359 	clockval = jiffies_to_clock_t(v_opts->br_mcast_ctx.multicast_startup_query_interval);
360 	if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_GOPTS_MCAST_STARTUP_QUERY_INTVL,
361 			      clockval, BRIDGE_VLANDB_GOPTS_PAD))
362 		goto out_err;
363 
364 	if (br_rports_have_mc_router(&v_opts->br_mcast_ctx)) {
365 		nest2 = nla_nest_start(skb,
366 				       BRIDGE_VLANDB_GOPTS_MCAST_ROUTER_PORTS);
367 		if (!nest2)
368 			goto out_err;
369 
370 		rcu_read_lock();
371 		if (br_rports_fill_info(skb, &v_opts->br_mcast_ctx)) {
372 			rcu_read_unlock();
373 			nla_nest_cancel(skb, nest2);
374 			goto out_err;
375 		}
376 		rcu_read_unlock();
377 
378 		nla_nest_end(skb, nest2);
379 	}
380 
381 #if IS_ENABLED(CONFIG_IPV6)
382 	if (nla_put_u8(skb, BRIDGE_VLANDB_GOPTS_MCAST_MLD_VERSION,
383 		       v_opts->br_mcast_ctx.multicast_mld_version))
384 		goto out_err;
385 #endif
386 #endif
387 
388 	if (nla_put_u16(skb, BRIDGE_VLANDB_GOPTS_MSTI, v_opts->msti))
389 		goto out_err;
390 
391 	nla_nest_end(skb, nest);
392 
393 	return true;
394 
395 out_err:
396 	nla_nest_cancel(skb, nest);
397 	return false;
398 }
399 
rtnl_vlan_global_opts_nlmsg_size(const struct net_bridge_vlan * v)400 static size_t rtnl_vlan_global_opts_nlmsg_size(const struct net_bridge_vlan *v)
401 {
402 	return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
403 		+ nla_total_size(0) /* BRIDGE_VLANDB_GLOBAL_OPTIONS */
404 		+ nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_GOPTS_ID */
405 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
406 		+ nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_GOPTS_MCAST_SNOOPING */
407 		+ nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_GOPTS_MCAST_IGMP_VERSION */
408 		+ nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_GOPTS_MCAST_MLD_VERSION */
409 		+ nla_total_size(sizeof(u32)) /* BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_CNT */
410 		+ nla_total_size(sizeof(u32)) /* BRIDGE_VLANDB_GOPTS_MCAST_STARTUP_QUERY_CNT */
411 		+ nla_total_size(sizeof(u64)) /* BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_INTVL */
412 		+ nla_total_size(sizeof(u64)) /* BRIDGE_VLANDB_GOPTS_MCAST_MEMBERSHIP_INTVL */
413 		+ nla_total_size(sizeof(u64)) /* BRIDGE_VLANDB_GOPTS_MCAST_QUERIER_INTVL */
414 		+ nla_total_size(sizeof(u64)) /* BRIDGE_VLANDB_GOPTS_MCAST_QUERY_INTVL */
415 		+ nla_total_size(sizeof(u64)) /* BRIDGE_VLANDB_GOPTS_MCAST_QUERY_RESPONSE_INTVL */
416 		+ nla_total_size(sizeof(u64)) /* BRIDGE_VLANDB_GOPTS_MCAST_STARTUP_QUERY_INTVL */
417 		+ nla_total_size(sizeof(u8)) /* BRIDGE_VLANDB_GOPTS_MCAST_QUERIER */
418 		+ br_multicast_querier_state_size() /* BRIDGE_VLANDB_GOPTS_MCAST_QUERIER_STATE */
419 		+ nla_total_size(0) /* BRIDGE_VLANDB_GOPTS_MCAST_ROUTER_PORTS */
420 		+ br_rports_size(&v->br_mcast_ctx) /* BRIDGE_VLANDB_GOPTS_MCAST_ROUTER_PORTS */
421 #endif
422 		+ nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_GOPTS_MSTI */
423 		+ nla_total_size(sizeof(u16)); /* BRIDGE_VLANDB_GOPTS_RANGE */
424 }
425 
br_vlan_global_opts_notify(const struct net_bridge * br,u16 vid,u16 vid_range)426 static void br_vlan_global_opts_notify(const struct net_bridge *br,
427 				       u16 vid, u16 vid_range)
428 {
429 	struct net_bridge_vlan *v;
430 	struct br_vlan_msg *bvm;
431 	struct nlmsghdr *nlh;
432 	struct sk_buff *skb;
433 	int err = -ENOBUFS;
434 
435 	/* right now notifications are done only with rtnl held */
436 	ASSERT_RTNL();
437 
438 	/* need to find the vlan due to flags/options */
439 	v = br_vlan_find(br_vlan_group(br), vid);
440 	if (!v)
441 		return;
442 
443 	skb = nlmsg_new(rtnl_vlan_global_opts_nlmsg_size(v), GFP_KERNEL);
444 	if (!skb)
445 		goto out_err;
446 
447 	err = -EMSGSIZE;
448 	nlh = nlmsg_put(skb, 0, 0, RTM_NEWVLAN, sizeof(*bvm), 0);
449 	if (!nlh)
450 		goto out_err;
451 	bvm = nlmsg_data(nlh);
452 	memset(bvm, 0, sizeof(*bvm));
453 	bvm->family = AF_BRIDGE;
454 	bvm->ifindex = br->dev->ifindex;
455 
456 	if (!br_vlan_global_opts_fill(skb, vid, vid_range, v))
457 		goto out_err;
458 
459 	nlmsg_end(skb, nlh);
460 	rtnl_notify(skb, dev_net(br->dev), 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
461 	return;
462 
463 out_err:
464 	rtnl_set_sk_err(dev_net(br->dev), RTNLGRP_BRVLAN, err);
465 	kfree_skb(skb);
466 }
467 
br_vlan_process_global_one_opts(const struct net_bridge * br,struct net_bridge_vlan_group * vg,struct net_bridge_vlan * v,struct nlattr ** tb,bool * changed,struct netlink_ext_ack * extack)468 static int br_vlan_process_global_one_opts(const struct net_bridge *br,
469 					   struct net_bridge_vlan_group *vg,
470 					   struct net_bridge_vlan *v,
471 					   struct nlattr **tb,
472 					   bool *changed,
473 					   struct netlink_ext_ack *extack)
474 {
475 	int err __maybe_unused;
476 
477 	*changed = false;
478 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
479 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_SNOOPING]) {
480 		u8 mc_snooping;
481 
482 		mc_snooping = nla_get_u8(tb[BRIDGE_VLANDB_GOPTS_MCAST_SNOOPING]);
483 		if (br_multicast_toggle_global_vlan(v, !!mc_snooping))
484 			*changed = true;
485 	}
486 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_IGMP_VERSION]) {
487 		u8 ver;
488 
489 		ver = nla_get_u8(tb[BRIDGE_VLANDB_GOPTS_MCAST_IGMP_VERSION]);
490 		err = br_multicast_set_igmp_version(&v->br_mcast_ctx, ver);
491 		if (err)
492 			return err;
493 		*changed = true;
494 	}
495 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_CNT]) {
496 		u32 cnt;
497 
498 		cnt = nla_get_u32(tb[BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_CNT]);
499 		v->br_mcast_ctx.multicast_last_member_count = cnt;
500 		*changed = true;
501 	}
502 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_STARTUP_QUERY_CNT]) {
503 		u32 cnt;
504 
505 		cnt = nla_get_u32(tb[BRIDGE_VLANDB_GOPTS_MCAST_STARTUP_QUERY_CNT]);
506 		v->br_mcast_ctx.multicast_startup_query_count = cnt;
507 		*changed = true;
508 	}
509 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_INTVL]) {
510 		u64 val;
511 
512 		val = nla_get_u64(tb[BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_INTVL]);
513 		v->br_mcast_ctx.multicast_last_member_interval = clock_t_to_jiffies(val);
514 		*changed = true;
515 	}
516 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_MEMBERSHIP_INTVL]) {
517 		u64 val;
518 
519 		val = nla_get_u64(tb[BRIDGE_VLANDB_GOPTS_MCAST_MEMBERSHIP_INTVL]);
520 		v->br_mcast_ctx.multicast_membership_interval = clock_t_to_jiffies(val);
521 		*changed = true;
522 	}
523 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_QUERIER_INTVL]) {
524 		u64 val;
525 
526 		val = nla_get_u64(tb[BRIDGE_VLANDB_GOPTS_MCAST_QUERIER_INTVL]);
527 		v->br_mcast_ctx.multicast_querier_interval = clock_t_to_jiffies(val);
528 		*changed = true;
529 	}
530 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_QUERY_INTVL]) {
531 		u64 val;
532 
533 		val = nla_get_u64(tb[BRIDGE_VLANDB_GOPTS_MCAST_QUERY_INTVL]);
534 		br_multicast_set_query_intvl(&v->br_mcast_ctx, val);
535 		*changed = true;
536 	}
537 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_QUERY_RESPONSE_INTVL]) {
538 		u64 val;
539 
540 		val = nla_get_u64(tb[BRIDGE_VLANDB_GOPTS_MCAST_QUERY_RESPONSE_INTVL]);
541 		v->br_mcast_ctx.multicast_query_response_interval = clock_t_to_jiffies(val);
542 		*changed = true;
543 	}
544 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_STARTUP_QUERY_INTVL]) {
545 		u64 val;
546 
547 		val = nla_get_u64(tb[BRIDGE_VLANDB_GOPTS_MCAST_STARTUP_QUERY_INTVL]);
548 		br_multicast_set_startup_query_intvl(&v->br_mcast_ctx, val);
549 		*changed = true;
550 	}
551 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_QUERIER]) {
552 		u8 val;
553 
554 		val = nla_get_u8(tb[BRIDGE_VLANDB_GOPTS_MCAST_QUERIER]);
555 		err = br_multicast_set_querier(&v->br_mcast_ctx, val);
556 		if (err)
557 			return err;
558 		*changed = true;
559 	}
560 #if IS_ENABLED(CONFIG_IPV6)
561 	if (tb[BRIDGE_VLANDB_GOPTS_MCAST_MLD_VERSION]) {
562 		u8 ver;
563 
564 		ver = nla_get_u8(tb[BRIDGE_VLANDB_GOPTS_MCAST_MLD_VERSION]);
565 		err = br_multicast_set_mld_version(&v->br_mcast_ctx, ver);
566 		if (err)
567 			return err;
568 		*changed = true;
569 	}
570 #endif
571 #endif
572 	if (tb[BRIDGE_VLANDB_GOPTS_MSTI]) {
573 		u16 msti;
574 
575 		msti = nla_get_u16(tb[BRIDGE_VLANDB_GOPTS_MSTI]);
576 		err = br_mst_vlan_set_msti(v, msti);
577 		if (err)
578 			return err;
579 		*changed = true;
580 	}
581 
582 	return 0;
583 }
584 
585 static const struct nla_policy br_vlan_db_gpol[BRIDGE_VLANDB_GOPTS_MAX + 1] = {
586 	[BRIDGE_VLANDB_GOPTS_ID]	= { .type = NLA_U16 },
587 	[BRIDGE_VLANDB_GOPTS_RANGE]	= { .type = NLA_U16 },
588 	[BRIDGE_VLANDB_GOPTS_MCAST_SNOOPING]	= { .type = NLA_U8 },
589 	[BRIDGE_VLANDB_GOPTS_MCAST_MLD_VERSION]	= { .type = NLA_U8 },
590 	[BRIDGE_VLANDB_GOPTS_MCAST_QUERY_INTVL]	= { .type = NLA_U64 },
591 	[BRIDGE_VLANDB_GOPTS_MCAST_QUERIER]	= { .type = NLA_U8 },
592 	[BRIDGE_VLANDB_GOPTS_MCAST_IGMP_VERSION]	= { .type = NLA_U8 },
593 	[BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_CNT]	= { .type = NLA_U32 },
594 	[BRIDGE_VLANDB_GOPTS_MCAST_STARTUP_QUERY_CNT]	= { .type = NLA_U32 },
595 	[BRIDGE_VLANDB_GOPTS_MCAST_LAST_MEMBER_INTVL]	= { .type = NLA_U64 },
596 	[BRIDGE_VLANDB_GOPTS_MCAST_MEMBERSHIP_INTVL]	= { .type = NLA_U64 },
597 	[BRIDGE_VLANDB_GOPTS_MCAST_QUERIER_INTVL]	= { .type = NLA_U64 },
598 	[BRIDGE_VLANDB_GOPTS_MCAST_STARTUP_QUERY_INTVL]	= { .type = NLA_U64 },
599 	[BRIDGE_VLANDB_GOPTS_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
600 	[BRIDGE_VLANDB_GOPTS_MSTI] = NLA_POLICY_MAX(NLA_U16, VLAN_N_VID - 1),
601 };
602 
br_vlan_rtm_process_global_options(struct net_device * dev,const struct nlattr * attr,int cmd,struct netlink_ext_ack * extack)603 int br_vlan_rtm_process_global_options(struct net_device *dev,
604 				       const struct nlattr *attr,
605 				       int cmd,
606 				       struct netlink_ext_ack *extack)
607 {
608 	struct net_bridge_vlan *v, *curr_start = NULL, *curr_end = NULL;
609 	struct nlattr *tb[BRIDGE_VLANDB_GOPTS_MAX + 1];
610 	struct net_bridge_vlan_group *vg;
611 	u16 vid, vid_range = 0;
612 	struct net_bridge *br;
613 	int err = 0;
614 
615 	if (cmd != RTM_NEWVLAN) {
616 		NL_SET_ERR_MSG_MOD(extack, "Global vlan options support only set operation");
617 		return -EINVAL;
618 	}
619 	if (!netif_is_bridge_master(dev)) {
620 		NL_SET_ERR_MSG_MOD(extack, "Global vlan options can only be set on bridge device");
621 		return -EINVAL;
622 	}
623 	br = netdev_priv(dev);
624 	vg = br_vlan_group(br);
625 	if (WARN_ON(!vg))
626 		return -ENODEV;
627 
628 	err = nla_parse_nested(tb, BRIDGE_VLANDB_GOPTS_MAX, attr,
629 			       br_vlan_db_gpol, extack);
630 	if (err)
631 		return err;
632 
633 	if (!tb[BRIDGE_VLANDB_GOPTS_ID]) {
634 		NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry id");
635 		return -EINVAL;
636 	}
637 	vid = nla_get_u16(tb[BRIDGE_VLANDB_GOPTS_ID]);
638 	if (!br_vlan_valid_id(vid, extack))
639 		return -EINVAL;
640 
641 	if (tb[BRIDGE_VLANDB_GOPTS_RANGE]) {
642 		vid_range = nla_get_u16(tb[BRIDGE_VLANDB_GOPTS_RANGE]);
643 		if (!br_vlan_valid_id(vid_range, extack))
644 			return -EINVAL;
645 		if (vid >= vid_range) {
646 			NL_SET_ERR_MSG_MOD(extack, "End vlan id is less than or equal to start vlan id");
647 			return -EINVAL;
648 		}
649 	} else {
650 		vid_range = vid;
651 	}
652 
653 	for (; vid <= vid_range; vid++) {
654 		bool changed = false;
655 
656 		v = br_vlan_find(vg, vid);
657 		if (!v) {
658 			NL_SET_ERR_MSG_MOD(extack, "Vlan in range doesn't exist, can't process global options");
659 			err = -ENOENT;
660 			break;
661 		}
662 
663 		err = br_vlan_process_global_one_opts(br, vg, v, tb, &changed,
664 						      extack);
665 		if (err)
666 			break;
667 
668 		if (changed) {
669 			/* vlan options changed, check for range */
670 			if (!curr_start) {
671 				curr_start = v;
672 				curr_end = v;
673 				continue;
674 			}
675 
676 			if (!br_vlan_global_opts_can_enter_range(v, curr_end)) {
677 				br_vlan_global_opts_notify(br, curr_start->vid,
678 							   curr_end->vid);
679 				curr_start = v;
680 			}
681 			curr_end = v;
682 		} else {
683 			/* nothing changed and nothing to notify yet */
684 			if (!curr_start)
685 				continue;
686 
687 			br_vlan_global_opts_notify(br, curr_start->vid,
688 						   curr_end->vid);
689 			curr_start = NULL;
690 			curr_end = NULL;
691 		}
692 	}
693 	if (curr_start)
694 		br_vlan_global_opts_notify(br, curr_start->vid, curr_end->vid);
695 
696 	return err;
697 }
698