1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <net/genetlink.h>
5 #include <uapi/linux/mrp_bridge.h>
6 #include "br_private.h"
7 #include "br_private_mrp.h"
9 static const struct nla_policy br_mrp_policy[IFLA_BRIDGE_MRP_MAX + 1] = {
10 [IFLA_BRIDGE_MRP_UNSPEC] = { .type = NLA_REJECT },
11 [IFLA_BRIDGE_MRP_INSTANCE] = { .type = NLA_NESTED },
12 [IFLA_BRIDGE_MRP_PORT_STATE] = { .type = NLA_NESTED },
13 [IFLA_BRIDGE_MRP_PORT_ROLE] = { .type = NLA_NESTED },
14 [IFLA_BRIDGE_MRP_RING_STATE] = { .type = NLA_NESTED },
15 [IFLA_BRIDGE_MRP_RING_ROLE] = { .type = NLA_NESTED },
16 [IFLA_BRIDGE_MRP_START_TEST] = { .type = NLA_NESTED },
17 [IFLA_BRIDGE_MRP_IN_ROLE] = { .type = NLA_NESTED },
18 [IFLA_BRIDGE_MRP_IN_STATE] = { .type = NLA_NESTED },
19 [IFLA_BRIDGE_MRP_START_IN_TEST] = { .type = NLA_NESTED },
22 static const struct nla_policy
23 br_mrp_instance_policy[IFLA_BRIDGE_MRP_INSTANCE_MAX + 1] = {
24 [IFLA_BRIDGE_MRP_INSTANCE_UNSPEC] = { .type = NLA_REJECT },
25 [IFLA_BRIDGE_MRP_INSTANCE_RING_ID] = { .type = NLA_U32 },
26 [IFLA_BRIDGE_MRP_INSTANCE_P_IFINDEX] = { .type = NLA_U32 },
27 [IFLA_BRIDGE_MRP_INSTANCE_S_IFINDEX] = { .type = NLA_U32 },
28 [IFLA_BRIDGE_MRP_INSTANCE_PRIO] = { .type = NLA_U16 },
31 static int br_mrp_instance_parse(struct net_bridge *br, struct nlattr *attr,
32 int cmd, struct netlink_ext_ack *extack)
34 struct nlattr *tb[IFLA_BRIDGE_MRP_INSTANCE_MAX + 1];
35 struct br_mrp_instance inst;
38 err = nla_parse_nested(tb, IFLA_BRIDGE_MRP_INSTANCE_MAX, attr,
39 br_mrp_instance_policy, extack);
43 if (!tb[IFLA_BRIDGE_MRP_INSTANCE_RING_ID] ||
44 !tb[IFLA_BRIDGE_MRP_INSTANCE_P_IFINDEX] ||
45 !tb[IFLA_BRIDGE_MRP_INSTANCE_S_IFINDEX]) {
46 NL_SET_ERR_MSG_MOD(extack,
47 "Missing attribute: RING_ID or P_IFINDEX or S_IFINDEX");
51 memset(&inst, 0, sizeof(inst));
53 inst.ring_id = nla_get_u32(tb[IFLA_BRIDGE_MRP_INSTANCE_RING_ID]);
54 inst.p_ifindex = nla_get_u32(tb[IFLA_BRIDGE_MRP_INSTANCE_P_IFINDEX]);
55 inst.s_ifindex = nla_get_u32(tb[IFLA_BRIDGE_MRP_INSTANCE_S_IFINDEX]);
56 inst.prio = MRP_DEFAULT_PRIO;
58 if (tb[IFLA_BRIDGE_MRP_INSTANCE_PRIO])
59 inst.prio = nla_get_u16(tb[IFLA_BRIDGE_MRP_INSTANCE_PRIO]);
61 if (cmd == RTM_SETLINK)
62 return br_mrp_add(br, &inst);
64 return br_mrp_del(br, &inst);
69 static const struct nla_policy
70 br_mrp_port_state_policy[IFLA_BRIDGE_MRP_PORT_STATE_MAX + 1] = {
71 [IFLA_BRIDGE_MRP_PORT_STATE_UNSPEC] = { .type = NLA_REJECT },
72 [IFLA_BRIDGE_MRP_PORT_STATE_STATE] = { .type = NLA_U32 },
75 static int br_mrp_port_state_parse(struct net_bridge_port *p,
77 struct netlink_ext_ack *extack)
79 struct nlattr *tb[IFLA_BRIDGE_MRP_PORT_STATE_MAX + 1];
80 enum br_mrp_port_state_type state;
83 err = nla_parse_nested(tb, IFLA_BRIDGE_MRP_PORT_STATE_MAX, attr,
84 br_mrp_port_state_policy, extack);
88 if (!tb[IFLA_BRIDGE_MRP_PORT_STATE_STATE]) {
89 NL_SET_ERR_MSG_MOD(extack, "Missing attribute: STATE");
93 state = nla_get_u32(tb[IFLA_BRIDGE_MRP_PORT_STATE_STATE]);
95 return br_mrp_set_port_state(p, state);
98 static const struct nla_policy
99 br_mrp_port_role_policy[IFLA_BRIDGE_MRP_PORT_ROLE_MAX + 1] = {
100 [IFLA_BRIDGE_MRP_PORT_ROLE_UNSPEC] = { .type = NLA_REJECT },
101 [IFLA_BRIDGE_MRP_PORT_ROLE_ROLE] = { .type = NLA_U32 },
104 static int br_mrp_port_role_parse(struct net_bridge_port *p,
106 struct netlink_ext_ack *extack)
108 struct nlattr *tb[IFLA_BRIDGE_MRP_PORT_ROLE_MAX + 1];
109 enum br_mrp_port_role_type role;
112 err = nla_parse_nested(tb, IFLA_BRIDGE_MRP_PORT_ROLE_MAX, attr,
113 br_mrp_port_role_policy, extack);
117 if (!tb[IFLA_BRIDGE_MRP_PORT_ROLE_ROLE]) {
118 NL_SET_ERR_MSG_MOD(extack, "Missing attribute: ROLE");
122 role = nla_get_u32(tb[IFLA_BRIDGE_MRP_PORT_ROLE_ROLE]);
124 return br_mrp_set_port_role(p, role);
127 static const struct nla_policy
128 br_mrp_ring_state_policy[IFLA_BRIDGE_MRP_RING_STATE_MAX + 1] = {
129 [IFLA_BRIDGE_MRP_RING_STATE_UNSPEC] = { .type = NLA_REJECT },
130 [IFLA_BRIDGE_MRP_RING_STATE_RING_ID] = { .type = NLA_U32 },
131 [IFLA_BRIDGE_MRP_RING_STATE_STATE] = { .type = NLA_U32 },
134 static int br_mrp_ring_state_parse(struct net_bridge *br, struct nlattr *attr,
135 struct netlink_ext_ack *extack)
137 struct nlattr *tb[IFLA_BRIDGE_MRP_RING_STATE_MAX + 1];
138 struct br_mrp_ring_state state;
141 err = nla_parse_nested(tb, IFLA_BRIDGE_MRP_RING_STATE_MAX, attr,
142 br_mrp_ring_state_policy, extack);
146 if (!tb[IFLA_BRIDGE_MRP_RING_STATE_RING_ID] ||
147 !tb[IFLA_BRIDGE_MRP_RING_STATE_STATE]) {
148 NL_SET_ERR_MSG_MOD(extack,
149 "Missing attribute: RING_ID or STATE");
153 memset(&state, 0x0, sizeof(state));
155 state.ring_id = nla_get_u32(tb[IFLA_BRIDGE_MRP_RING_STATE_RING_ID]);
156 state.ring_state = nla_get_u32(tb[IFLA_BRIDGE_MRP_RING_STATE_STATE]);
158 return br_mrp_set_ring_state(br, &state);
161 static const struct nla_policy
162 br_mrp_ring_role_policy[IFLA_BRIDGE_MRP_RING_ROLE_MAX + 1] = {
163 [IFLA_BRIDGE_MRP_RING_ROLE_UNSPEC] = { .type = NLA_REJECT },
164 [IFLA_BRIDGE_MRP_RING_ROLE_RING_ID] = { .type = NLA_U32 },
165 [IFLA_BRIDGE_MRP_RING_ROLE_ROLE] = { .type = NLA_U32 },
168 static int br_mrp_ring_role_parse(struct net_bridge *br, struct nlattr *attr,
169 struct netlink_ext_ack *extack)
171 struct nlattr *tb[IFLA_BRIDGE_MRP_RING_ROLE_MAX + 1];
172 struct br_mrp_ring_role role;
175 err = nla_parse_nested(tb, IFLA_BRIDGE_MRP_RING_ROLE_MAX, attr,
176 br_mrp_ring_role_policy, extack);
180 if (!tb[IFLA_BRIDGE_MRP_RING_ROLE_RING_ID] ||
181 !tb[IFLA_BRIDGE_MRP_RING_ROLE_ROLE]) {
182 NL_SET_ERR_MSG_MOD(extack,
183 "Missing attribute: RING_ID or ROLE");
187 memset(&role, 0x0, sizeof(role));
189 role.ring_id = nla_get_u32(tb[IFLA_BRIDGE_MRP_RING_ROLE_RING_ID]);
190 role.ring_role = nla_get_u32(tb[IFLA_BRIDGE_MRP_RING_ROLE_ROLE]);
192 return br_mrp_set_ring_role(br, &role);
195 static const struct nla_policy
196 br_mrp_start_test_policy[IFLA_BRIDGE_MRP_START_TEST_MAX + 1] = {
197 [IFLA_BRIDGE_MRP_START_TEST_UNSPEC] = { .type = NLA_REJECT },
198 [IFLA_BRIDGE_MRP_START_TEST_RING_ID] = { .type = NLA_U32 },
199 [IFLA_BRIDGE_MRP_START_TEST_INTERVAL] = { .type = NLA_U32 },
200 [IFLA_BRIDGE_MRP_START_TEST_MAX_MISS] = { .type = NLA_U32 },
201 [IFLA_BRIDGE_MRP_START_TEST_PERIOD] = { .type = NLA_U32 },
202 [IFLA_BRIDGE_MRP_START_TEST_MONITOR] = { .type = NLA_U32 },
205 static int br_mrp_start_test_parse(struct net_bridge *br, struct nlattr *attr,
206 struct netlink_ext_ack *extack)
208 struct nlattr *tb[IFLA_BRIDGE_MRP_START_TEST_MAX + 1];
209 struct br_mrp_start_test test;
212 err = nla_parse_nested(tb, IFLA_BRIDGE_MRP_START_TEST_MAX, attr,
213 br_mrp_start_test_policy, extack);
217 if (!tb[IFLA_BRIDGE_MRP_START_TEST_RING_ID] ||
218 !tb[IFLA_BRIDGE_MRP_START_TEST_INTERVAL] ||
219 !tb[IFLA_BRIDGE_MRP_START_TEST_MAX_MISS] ||
220 !tb[IFLA_BRIDGE_MRP_START_TEST_PERIOD]) {
221 NL_SET_ERR_MSG_MOD(extack,
222 "Missing attribute: RING_ID or INTERVAL or MAX_MISS or PERIOD");
226 memset(&test, 0x0, sizeof(test));
228 test.ring_id = nla_get_u32(tb[IFLA_BRIDGE_MRP_START_TEST_RING_ID]);
229 test.interval = nla_get_u32(tb[IFLA_BRIDGE_MRP_START_TEST_INTERVAL]);
230 test.max_miss = nla_get_u32(tb[IFLA_BRIDGE_MRP_START_TEST_MAX_MISS]);
231 test.period = nla_get_u32(tb[IFLA_BRIDGE_MRP_START_TEST_PERIOD]);
232 test.monitor = false;
234 if (tb[IFLA_BRIDGE_MRP_START_TEST_MONITOR])
236 nla_get_u32(tb[IFLA_BRIDGE_MRP_START_TEST_MONITOR]);
238 return br_mrp_start_test(br, &test);
241 static const struct nla_policy
242 br_mrp_in_state_policy[IFLA_BRIDGE_MRP_IN_STATE_MAX + 1] = {
243 [IFLA_BRIDGE_MRP_IN_STATE_UNSPEC] = { .type = NLA_REJECT },
244 [IFLA_BRIDGE_MRP_IN_STATE_IN_ID] = { .type = NLA_U32 },
245 [IFLA_BRIDGE_MRP_IN_STATE_STATE] = { .type = NLA_U32 },
248 static int br_mrp_in_state_parse(struct net_bridge *br, struct nlattr *attr,
249 struct netlink_ext_ack *extack)
251 struct nlattr *tb[IFLA_BRIDGE_MRP_IN_STATE_MAX + 1];
252 struct br_mrp_in_state state;
255 err = nla_parse_nested(tb, IFLA_BRIDGE_MRP_IN_STATE_MAX, attr,
256 br_mrp_in_state_policy, extack);
260 if (!tb[IFLA_BRIDGE_MRP_IN_STATE_IN_ID] ||
261 !tb[IFLA_BRIDGE_MRP_IN_STATE_STATE]) {
262 NL_SET_ERR_MSG_MOD(extack,
263 "Missing attribute: IN_ID or STATE");
267 memset(&state, 0x0, sizeof(state));
269 state.in_id = nla_get_u32(tb[IFLA_BRIDGE_MRP_IN_STATE_IN_ID]);
270 state.in_state = nla_get_u32(tb[IFLA_BRIDGE_MRP_IN_STATE_STATE]);
272 return br_mrp_set_in_state(br, &state);
275 static const struct nla_policy
276 br_mrp_in_role_policy[IFLA_BRIDGE_MRP_IN_ROLE_MAX + 1] = {
277 [IFLA_BRIDGE_MRP_IN_ROLE_UNSPEC] = { .type = NLA_REJECT },
278 [IFLA_BRIDGE_MRP_IN_ROLE_RING_ID] = { .type = NLA_U32 },
279 [IFLA_BRIDGE_MRP_IN_ROLE_IN_ID] = { .type = NLA_U16 },
280 [IFLA_BRIDGE_MRP_IN_ROLE_ROLE] = { .type = NLA_U32 },
281 [IFLA_BRIDGE_MRP_IN_ROLE_I_IFINDEX] = { .type = NLA_U32 },
284 static int br_mrp_in_role_parse(struct net_bridge *br, struct nlattr *attr,
285 struct netlink_ext_ack *extack)
287 struct nlattr *tb[IFLA_BRIDGE_MRP_IN_ROLE_MAX + 1];
288 struct br_mrp_in_role role;
291 err = nla_parse_nested(tb, IFLA_BRIDGE_MRP_IN_ROLE_MAX, attr,
292 br_mrp_in_role_policy, extack);
296 if (!tb[IFLA_BRIDGE_MRP_IN_ROLE_RING_ID] ||
297 !tb[IFLA_BRIDGE_MRP_IN_ROLE_IN_ID] ||
298 !tb[IFLA_BRIDGE_MRP_IN_ROLE_I_IFINDEX] ||
299 !tb[IFLA_BRIDGE_MRP_IN_ROLE_ROLE]) {
300 NL_SET_ERR_MSG_MOD(extack,
301 "Missing attribute: RING_ID or ROLE or IN_ID or I_IFINDEX");
305 memset(&role, 0x0, sizeof(role));
307 role.ring_id = nla_get_u32(tb[IFLA_BRIDGE_MRP_IN_ROLE_RING_ID]);
308 role.in_id = nla_get_u16(tb[IFLA_BRIDGE_MRP_IN_ROLE_IN_ID]);
309 role.i_ifindex = nla_get_u32(tb[IFLA_BRIDGE_MRP_IN_ROLE_I_IFINDEX]);
310 role.in_role = nla_get_u32(tb[IFLA_BRIDGE_MRP_IN_ROLE_ROLE]);
312 return br_mrp_set_in_role(br, &role);
315 static const struct nla_policy
316 br_mrp_start_in_test_policy[IFLA_BRIDGE_MRP_START_IN_TEST_MAX + 1] = {
317 [IFLA_BRIDGE_MRP_START_IN_TEST_UNSPEC] = { .type = NLA_REJECT },
318 [IFLA_BRIDGE_MRP_START_IN_TEST_IN_ID] = { .type = NLA_U32 },
319 [IFLA_BRIDGE_MRP_START_IN_TEST_INTERVAL] = { .type = NLA_U32 },
320 [IFLA_BRIDGE_MRP_START_IN_TEST_MAX_MISS] = { .type = NLA_U32 },
321 [IFLA_BRIDGE_MRP_START_IN_TEST_PERIOD] = { .type = NLA_U32 },
324 static int br_mrp_start_in_test_parse(struct net_bridge *br,
326 struct netlink_ext_ack *extack)
328 struct nlattr *tb[IFLA_BRIDGE_MRP_START_IN_TEST_MAX + 1];
329 struct br_mrp_start_in_test test;
332 err = nla_parse_nested(tb, IFLA_BRIDGE_MRP_START_IN_TEST_MAX, attr,
333 br_mrp_start_in_test_policy, extack);
337 if (!tb[IFLA_BRIDGE_MRP_START_IN_TEST_IN_ID] ||
338 !tb[IFLA_BRIDGE_MRP_START_IN_TEST_INTERVAL] ||
339 !tb[IFLA_BRIDGE_MRP_START_IN_TEST_MAX_MISS] ||
340 !tb[IFLA_BRIDGE_MRP_START_IN_TEST_PERIOD]) {
341 NL_SET_ERR_MSG_MOD(extack,
342 "Missing attribute: RING_ID or INTERVAL or MAX_MISS or PERIOD");
346 memset(&test, 0x0, sizeof(test));
348 test.in_id = nla_get_u32(tb[IFLA_BRIDGE_MRP_START_IN_TEST_IN_ID]);
349 test.interval = nla_get_u32(tb[IFLA_BRIDGE_MRP_START_IN_TEST_INTERVAL]);
350 test.max_miss = nla_get_u32(tb[IFLA_BRIDGE_MRP_START_IN_TEST_MAX_MISS]);
351 test.period = nla_get_u32(tb[IFLA_BRIDGE_MRP_START_IN_TEST_PERIOD]);
353 return br_mrp_start_in_test(br, &test);
356 int br_mrp_parse(struct net_bridge *br, struct net_bridge_port *p,
357 struct nlattr *attr, int cmd, struct netlink_ext_ack *extack)
359 struct nlattr *tb[IFLA_BRIDGE_MRP_MAX + 1];
362 /* When this function is called for a port then the br pointer is
363 * invalid, therefor set the br to point correctly
368 if (br->stp_enabled != BR_NO_STP) {
369 NL_SET_ERR_MSG_MOD(extack, "MRP can't be enabled if STP is already enabled");
373 err = nla_parse_nested(tb, IFLA_BRIDGE_MRP_MAX, attr,
374 br_mrp_policy, extack);
378 if (tb[IFLA_BRIDGE_MRP_INSTANCE]) {
379 err = br_mrp_instance_parse(br, tb[IFLA_BRIDGE_MRP_INSTANCE],
385 if (tb[IFLA_BRIDGE_MRP_PORT_STATE]) {
386 err = br_mrp_port_state_parse(p, tb[IFLA_BRIDGE_MRP_PORT_STATE],
392 if (tb[IFLA_BRIDGE_MRP_PORT_ROLE]) {
393 err = br_mrp_port_role_parse(p, tb[IFLA_BRIDGE_MRP_PORT_ROLE],
399 if (tb[IFLA_BRIDGE_MRP_RING_STATE]) {
400 err = br_mrp_ring_state_parse(br,
401 tb[IFLA_BRIDGE_MRP_RING_STATE],
407 if (tb[IFLA_BRIDGE_MRP_RING_ROLE]) {
408 err = br_mrp_ring_role_parse(br, tb[IFLA_BRIDGE_MRP_RING_ROLE],
414 if (tb[IFLA_BRIDGE_MRP_START_TEST]) {
415 err = br_mrp_start_test_parse(br,
416 tb[IFLA_BRIDGE_MRP_START_TEST],
422 if (tb[IFLA_BRIDGE_MRP_IN_STATE]) {
423 err = br_mrp_in_state_parse(br, tb[IFLA_BRIDGE_MRP_IN_STATE],
429 if (tb[IFLA_BRIDGE_MRP_IN_ROLE]) {
430 err = br_mrp_in_role_parse(br, tb[IFLA_BRIDGE_MRP_IN_ROLE],
436 if (tb[IFLA_BRIDGE_MRP_START_IN_TEST]) {
437 err = br_mrp_start_in_test_parse(br,
438 tb[IFLA_BRIDGE_MRP_START_IN_TEST],
447 int br_mrp_fill_info(struct sk_buff *skb, struct net_bridge *br)
449 struct nlattr *tb, *mrp_tb;
452 mrp_tb = nla_nest_start_noflag(skb, IFLA_BRIDGE_MRP);
456 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list) {
457 struct net_bridge_port *p;
459 tb = nla_nest_start_noflag(skb, IFLA_BRIDGE_MRP_INFO);
461 goto nla_info_failure;
463 if (nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_RING_ID,
465 goto nla_put_failure;
467 p = rcu_dereference(mrp->p_port);
468 if (p && nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_P_IFINDEX,
470 goto nla_put_failure;
472 p = rcu_dereference(mrp->s_port);
473 if (p && nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_S_IFINDEX,
475 goto nla_put_failure;
477 p = rcu_dereference(mrp->i_port);
478 if (p && nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_I_IFINDEX,
480 goto nla_put_failure;
482 if (nla_put_u16(skb, IFLA_BRIDGE_MRP_INFO_PRIO,
484 goto nla_put_failure;
485 if (nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_RING_STATE,
487 goto nla_put_failure;
488 if (nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_RING_ROLE,
490 goto nla_put_failure;
491 if (nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_TEST_INTERVAL,
493 goto nla_put_failure;
494 if (nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_TEST_MAX_MISS,
496 goto nla_put_failure;
497 if (nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_TEST_MONITOR,
499 goto nla_put_failure;
501 if (nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_IN_STATE,
503 goto nla_put_failure;
504 if (nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_IN_ROLE,
506 goto nla_put_failure;
507 if (nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_IN_TEST_INTERVAL,
508 mrp->in_test_interval))
509 goto nla_put_failure;
510 if (nla_put_u32(skb, IFLA_BRIDGE_MRP_INFO_IN_TEST_MAX_MISS,
511 mrp->in_test_max_miss))
512 goto nla_put_failure;
514 nla_nest_end(skb, tb);
516 nla_nest_end(skb, mrp_tb);
521 nla_nest_cancel(skb, tb);
524 nla_nest_cancel(skb, mrp_tb);
529 int br_mrp_ring_port_open(struct net_device *dev, u8 loc)
531 struct net_bridge_port *p;
534 p = br_port_get_rcu(dev);
541 p->flags |= BR_MRP_LOST_CONT;
543 p->flags &= ~BR_MRP_LOST_CONT;
545 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
551 int br_mrp_in_port_open(struct net_device *dev, u8 loc)
553 struct net_bridge_port *p;
556 p = br_port_get_rcu(dev);
563 p->flags |= BR_MRP_LOST_IN_CONT;
565 p->flags &= ~BR_MRP_LOST_IN_CONT;
567 br_ifinfo_notify(RTM_NEWLINK, NULL, p);