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