1 /* SPDX-License-Identifier: GPL-2.0 */
7 #include <sys/socket.h>
9 #include <netinet/in.h>
11 #include <linux/if_bridge.h>
15 #include "json_print.h"
16 #include "libnetlink.h"
18 #include "br_common.h"
20 static unsigned int filter_index
;
22 static const char *port_states
[] = {
23 [BR_STATE_DISABLED
] = "disabled",
24 [BR_STATE_LISTENING
] = "listening",
25 [BR_STATE_LEARNING
] = "learning",
26 [BR_STATE_FORWARDING
] = "forwarding",
27 [BR_STATE_BLOCKING
] = "blocking",
30 static const char *hw_mode
[] = {
34 static void print_link_flags(FILE *fp
, unsigned int flags
, unsigned int mdown
)
36 open_json_array(PRINT_ANY
, is_json_context() ? "flags" : "<");
37 if (flags
& IFF_UP
&& !(flags
& IFF_RUNNING
))
38 print_string(PRINT_ANY
, NULL
,
39 flags
? "%s," : "%s", "NO-CARRIER");
40 flags
&= ~IFF_RUNNING
;
42 #define _PF(f) if (flags&IFF_##f) { \
44 print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); }
65 print_hex(PRINT_ANY
, NULL
, "%x", flags
);
67 print_string(PRINT_ANY
, NULL
, ",%s", "M-DOWN");
68 close_json_array(PRINT_ANY
, "> ");
71 static void print_portstate(__u8 state
)
73 if (state
<= BR_STATE_BLOCKING
)
74 print_string(PRINT_ANY
, "state",
75 "state %s ", port_states
[state
]);
77 print_uint(PRINT_ANY
, "state",
78 "state (%d) ", state
);
81 static void print_onoff(FILE *fp
, const char *flag
, __u8 val
)
83 if (is_json_context())
84 print_bool(PRINT_JSON
, flag
, NULL
, val
);
86 fprintf(fp
, "%s %s ", flag
, val
? "on" : "off");
89 static void print_hwmode(__u16 mode
)
91 if (mode
>= ARRAY_SIZE(hw_mode
))
92 print_0xhex(PRINT_ANY
, "hwmode",
93 "hwmode %#llx ", mode
);
95 print_string(PRINT_ANY
, "hwmode",
96 "hwmode %s ", hw_mode
[mode
]);
99 static void print_protinfo(FILE *fp
, struct rtattr
*attr
)
101 if (attr
->rta_type
& NLA_F_NESTED
) {
102 struct rtattr
*prtb
[IFLA_BRPORT_MAX
+ 1];
104 parse_rtattr_nested(prtb
, IFLA_BRPORT_MAX
, attr
);
106 if (prtb
[IFLA_BRPORT_STATE
])
107 print_portstate(rta_getattr_u8(prtb
[IFLA_BRPORT_STATE
]));
109 if (prtb
[IFLA_BRPORT_PRIORITY
])
110 print_uint(PRINT_ANY
, "priority",
112 rta_getattr_u16(prtb
[IFLA_BRPORT_PRIORITY
]));
114 if (prtb
[IFLA_BRPORT_COST
])
115 print_uint(PRINT_ANY
, "cost",
117 rta_getattr_u32(prtb
[IFLA_BRPORT_COST
]));
122 if (!is_json_context())
123 fprintf(fp
, "%s ", _SL_
);
125 if (prtb
[IFLA_BRPORT_MODE
])
126 print_onoff(fp
, "hairpin",
127 rta_getattr_u8(prtb
[IFLA_BRPORT_MODE
]));
128 if (prtb
[IFLA_BRPORT_GUARD
])
129 print_onoff(fp
, "guard",
130 rta_getattr_u8(prtb
[IFLA_BRPORT_GUARD
]));
131 if (prtb
[IFLA_BRPORT_PROTECT
])
132 print_onoff(fp
, "root_block",
133 rta_getattr_u8(prtb
[IFLA_BRPORT_PROTECT
]));
134 if (prtb
[IFLA_BRPORT_FAST_LEAVE
])
135 print_onoff(fp
, "fastleave",
136 rta_getattr_u8(prtb
[IFLA_BRPORT_FAST_LEAVE
]));
137 if (prtb
[IFLA_BRPORT_LEARNING
])
138 print_onoff(fp
, "learning",
139 rta_getattr_u8(prtb
[IFLA_BRPORT_LEARNING
]));
140 if (prtb
[IFLA_BRPORT_LEARNING_SYNC
])
141 print_onoff(fp
, "learning_sync",
142 rta_getattr_u8(prtb
[IFLA_BRPORT_LEARNING_SYNC
]));
143 if (prtb
[IFLA_BRPORT_UNICAST_FLOOD
])
144 print_onoff(fp
, "flood",
145 rta_getattr_u8(prtb
[IFLA_BRPORT_UNICAST_FLOOD
]));
146 if (prtb
[IFLA_BRPORT_MCAST_FLOOD
])
147 print_onoff(fp
, "mcast_flood",
148 rta_getattr_u8(prtb
[IFLA_BRPORT_MCAST_FLOOD
]));
149 if (prtb
[IFLA_BRPORT_MCAST_TO_UCAST
])
150 print_onoff(fp
, "mcast_to_unicast",
151 rta_getattr_u8(prtb
[IFLA_BRPORT_MCAST_TO_UCAST
]));
152 if (prtb
[IFLA_BRPORT_NEIGH_SUPPRESS
])
153 print_onoff(fp
, "neigh_suppress",
154 rta_getattr_u8(prtb
[IFLA_BRPORT_NEIGH_SUPPRESS
]));
155 if (prtb
[IFLA_BRPORT_VLAN_TUNNEL
])
156 print_onoff(fp
, "vlan_tunnel",
157 rta_getattr_u8(prtb
[IFLA_BRPORT_VLAN_TUNNEL
]));
159 if (prtb
[IFLA_BRPORT_BACKUP_PORT
]) {
162 ifidx
= rta_getattr_u32(prtb
[IFLA_BRPORT_BACKUP_PORT
]);
163 print_string(PRINT_ANY
,
164 "backup_port", "backup_port %s ",
165 ll_index_to_name(ifidx
));
168 if (prtb
[IFLA_BRPORT_ISOLATED
])
169 print_onoff(fp
, "isolated",
170 rta_getattr_u8(prtb
[IFLA_BRPORT_ISOLATED
]));
172 print_portstate(rta_getattr_u8(attr
));
177 * This is reported by HW devices that have some bridging
180 static void print_af_spec(struct rtattr
*attr
, int ifindex
)
182 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+1];
184 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, attr
);
186 if (aftb
[IFLA_BRIDGE_MODE
])
187 print_hwmode(rta_getattr_u16(aftb
[IFLA_BRIDGE_MODE
]));
192 if (aftb
[IFLA_BRIDGE_VLAN_INFO
])
193 print_vlan_info(aftb
[IFLA_BRIDGE_VLAN_INFO
], ifindex
);
196 int print_linkinfo(struct nlmsghdr
*n
, void *arg
)
199 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
200 struct rtattr
*tb
[IFLA_MAX
+1];
201 unsigned int m_flag
= 0;
202 int len
= n
->nlmsg_len
;
205 len
-= NLMSG_LENGTH(sizeof(*ifi
));
207 fprintf(stderr
, "Message too short!\n");
211 if (!(ifi
->ifi_family
== AF_BRIDGE
|| ifi
->ifi_family
== AF_UNSPEC
))
214 if (filter_index
&& filter_index
!= ifi
->ifi_index
)
217 parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
, NLA_F_NESTED
);
219 name
= get_ifname_rta(ifi
->ifi_index
, tb
[IFLA_IFNAME
]);
223 open_json_object(NULL
);
224 if (n
->nlmsg_type
== RTM_DELLINK
)
225 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
227 print_int(PRINT_ANY
, "ifindex", "%d: ", ifi
->ifi_index
);
228 m_flag
= print_name_and_link("%s: ", name
, tb
);
229 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
234 rta_getattr_u32(tb
[IFLA_MTU
]));
236 if (tb
[IFLA_MASTER
]) {
237 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
239 print_string(PRINT_ANY
, "master", "master %s ",
240 ll_index_to_name(master
));
243 if (tb
[IFLA_PROTINFO
])
244 print_protinfo(fp
, tb
[IFLA_PROTINFO
]);
246 if (tb
[IFLA_AF_SPEC
])
247 print_af_spec(tb
[IFLA_AF_SPEC
], ifi
->ifi_index
);
249 print_string(PRINT_FP
, NULL
, "%s", "\n");
255 static void usage(void)
257 fprintf(stderr
, "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n");
258 fprintf(stderr
, " [ guard {on | off} ]\n");
259 fprintf(stderr
, " [ hairpin {on | off} ]\n");
260 fprintf(stderr
, " [ fastleave {on | off} ]\n");
261 fprintf(stderr
, " [ root_block {on | off} ]\n");
262 fprintf(stderr
, " [ learning {on | off} ]\n");
263 fprintf(stderr
, " [ learning_sync {on | off} ]\n");
264 fprintf(stderr
, " [ flood {on | off} ]\n");
265 fprintf(stderr
, " [ mcast_flood {on | off} ]\n");
266 fprintf(stderr
, " [ mcast_to_unicast {on | off} ]\n");
267 fprintf(stderr
, " [ neigh_suppress {on | off} ]\n");
268 fprintf(stderr
, " [ vlan_tunnel {on | off} ]\n");
269 fprintf(stderr
, " [ isolated {on | off} ]\n");
270 fprintf(stderr
, " [ hwmode {vepa | veb} ]\n");
271 fprintf(stderr
, " [ backup_port DEVICE ] [ nobackup_port ]\n");
272 fprintf(stderr
, " [ self ] [ master ]\n");
273 fprintf(stderr
, " bridge link show [dev DEV]\n");
277 static bool on_off(char *arg
, __s8
*attr
, char *val
)
279 if (strcmp(val
, "on") == 0)
281 else if (strcmp(val
, "off") == 0)
285 "Error: argument of \"%s\" must be \"on\" or \"off\"\n",
293 static int brlink_modify(int argc
, char **argv
)
297 struct ifinfomsg ifm
;
300 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
301 .n
.nlmsg_flags
= NLM_F_REQUEST
,
302 .n
.nlmsg_type
= RTM_SETLINK
,
303 .ifm
.ifi_family
= PF_BRIDGE
,
306 int backup_port_idx
= -1;
307 __s8 neigh_suppress
= -1;
309 __s8 learning_sync
= -1;
311 __s8 vlan_tunnel
= -1;
312 __s8 mcast_flood
= -1;
313 __s8 mcast_to_unicast
= -1;
316 __s8 bpdu_guard
= -1;
317 __s8 fast_leave
= -1;
318 __s8 root_block
= -1;
327 if (strcmp(*argv
, "dev") == 0) {
330 } else if (strcmp(*argv
, "guard") == 0) {
332 if (!on_off("guard", &bpdu_guard
, *argv
))
334 } else if (strcmp(*argv
, "hairpin") == 0) {
336 if (!on_off("hairpin", &hairpin
, *argv
))
338 } else if (strcmp(*argv
, "fastleave") == 0) {
340 if (!on_off("fastleave", &fast_leave
, *argv
))
342 } else if (strcmp(*argv
, "root_block") == 0) {
344 if (!on_off("root_block", &root_block
, *argv
))
346 } else if (strcmp(*argv
, "learning") == 0) {
348 if (!on_off("learning", &learning
, *argv
))
350 } else if (strcmp(*argv
, "learning_sync") == 0) {
352 if (!on_off("learning_sync", &learning_sync
, *argv
))
354 } else if (strcmp(*argv
, "flood") == 0) {
356 if (!on_off("flood", &flood
, *argv
))
358 } else if (strcmp(*argv
, "mcast_flood") == 0) {
360 if (!on_off("mcast_flood", &mcast_flood
, *argv
))
362 } else if (strcmp(*argv
, "mcast_to_unicast") == 0) {
364 if (!on_off("mcast_to_unicast", &mcast_to_unicast
, *argv
))
366 } else if (strcmp(*argv
, "cost") == 0) {
369 } else if (strcmp(*argv
, "priority") == 0) {
371 priority
= atoi(*argv
);
372 } else if (strcmp(*argv
, "state") == 0) {
375 size_t nstates
= ARRAY_SIZE(port_states
);
377 state
= strtol(*argv
, &endptr
, 10);
378 if (!(**argv
!= '\0' && *endptr
== '\0')) {
379 for (state
= 0; state
< nstates
; state
++)
380 if (strcmp(port_states
[state
], *argv
) == 0)
382 if (state
== nstates
) {
384 "Error: invalid STP port state\n");
388 } else if (strcmp(*argv
, "hwmode") == 0) {
390 flags
= BRIDGE_FLAGS_SELF
;
391 if (strcmp(*argv
, "vepa") == 0)
392 mode
= BRIDGE_MODE_VEPA
;
393 else if (strcmp(*argv
, "veb") == 0)
394 mode
= BRIDGE_MODE_VEB
;
397 "Mode argument must be \"vepa\" or \"veb\".\n");
400 } else if (strcmp(*argv
, "self") == 0) {
401 flags
|= BRIDGE_FLAGS_SELF
;
402 } else if (strcmp(*argv
, "master") == 0) {
403 flags
|= BRIDGE_FLAGS_MASTER
;
404 } else if (strcmp(*argv
, "neigh_suppress") == 0) {
406 if (!on_off("neigh_suppress", &neigh_suppress
,
409 } else if (strcmp(*argv
, "vlan_tunnel") == 0) {
411 if (!on_off("vlan_tunnel", &vlan_tunnel
,
414 } else if (strcmp(*argv
, "isolated") == 0) {
416 if (!on_off("isolated", &isolated
, *argv
))
418 } else if (strcmp(*argv
, "backup_port") == 0) {
420 backup_port_idx
= ll_name_to_index(*argv
);
421 if (!backup_port_idx
) {
422 fprintf(stderr
, "Error: device %s does not exist\n",
426 } else if (strcmp(*argv
, "nobackup_port") == 0) {
434 fprintf(stderr
, "Device is a required argument.\n");
439 req
.ifm
.ifi_index
= ll_name_to_index(d
);
440 if (req
.ifm
.ifi_index
== 0) {
441 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", d
);
445 /* Nested PROTINFO attribute. Contains: port flags, cost, priority and
448 nest
= addattr_nest(&req
.n
, sizeof(req
),
449 IFLA_PROTINFO
| NLA_F_NESTED
);
452 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_GUARD
, bpdu_guard
);
454 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_MODE
, hairpin
);
456 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_FAST_LEAVE
,
459 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_PROTECT
, root_block
);
461 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_UNICAST_FLOOD
, flood
);
462 if (mcast_flood
>= 0)
463 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_MCAST_FLOOD
,
465 if (mcast_to_unicast
>= 0)
466 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_MCAST_TO_UCAST
,
469 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_LEARNING
, learning
);
470 if (learning_sync
>= 0)
471 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_LEARNING_SYNC
,
475 addattr32(&req
.n
, sizeof(req
), IFLA_BRPORT_COST
, cost
);
478 addattr16(&req
.n
, sizeof(req
), IFLA_BRPORT_PRIORITY
, priority
);
481 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_STATE
, state
);
483 if (neigh_suppress
!= -1)
484 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_NEIGH_SUPPRESS
,
486 if (vlan_tunnel
!= -1)
487 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_VLAN_TUNNEL
,
490 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_ISOLATED
, isolated
);
492 if (backup_port_idx
!= -1)
493 addattr32(&req
.n
, sizeof(req
), IFLA_BRPORT_BACKUP_PORT
,
496 addattr_nest_end(&req
.n
, nest
);
498 /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that
499 * designates master or self operation and IFLA_BRIDGE_MODE
500 * for hw 'vepa' or 'veb' operation modes. The hwmodes are
501 * only valid in 'self' mode on some devices so far.
503 if (mode
>= 0 || flags
> 0) {
504 nest
= addattr_nest(&req
.n
, sizeof(req
), IFLA_AF_SPEC
);
507 addattr16(&req
.n
, sizeof(req
), IFLA_BRIDGE_FLAGS
, flags
);
510 addattr16(&req
.n
, sizeof(req
), IFLA_BRIDGE_MODE
, mode
);
512 addattr_nest_end(&req
.n
, nest
);
515 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
521 static int brlink_show(int argc
, char **argv
)
523 char *filter_dev
= NULL
;
526 if (strcmp(*argv
, "dev") == 0) {
529 duparg("dev", *argv
);
536 filter_index
= ll_name_to_index(filter_dev
);
538 return nodev(filter_dev
);
542 if (rtnl_linkdump_req_filter(&rth
, PF_BRIDGE
,
544 RTEXT_FILTER_BRVLAN_COMPRESSED
:
545 RTEXT_FILTER_BRVLAN
)) < 0) {
546 perror("Cannon send dump request");
550 if (rtnl_linkdump_req(&rth
, PF_BRIDGE
) < 0) {
551 perror("Cannon send dump request");
557 if (rtnl_dump_filter(&rth
, print_linkinfo
, stdout
) < 0) {
558 fprintf(stderr
, "Dump terminated\n");
567 int do_link(int argc
, char **argv
)
571 if (matches(*argv
, "set") == 0 ||
572 matches(*argv
, "change") == 0)
573 return brlink_modify(argc
-1, argv
+1);
574 if (matches(*argv
, "show") == 0 ||
575 matches(*argv
, "lst") == 0 ||
576 matches(*argv
, "list") == 0)
577 return brlink_show(argc
-1, argv
+1);
578 if (matches(*argv
, "help") == 0)
581 return brlink_show(0, NULL
);
583 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv
);