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 %#hx ", 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_NEIGH_SUPPRESS
])
150 print_onoff(fp
, "neigh_suppress",
151 rta_getattr_u8(prtb
[IFLA_BRPORT_NEIGH_SUPPRESS
]));
152 if (prtb
[IFLA_BRPORT_VLAN_TUNNEL
])
153 print_onoff(fp
, "vlan_tunnel",
154 rta_getattr_u8(prtb
[IFLA_BRPORT_VLAN_TUNNEL
]));
156 print_portstate(rta_getattr_u8(attr
));
161 * This is reported by HW devices that have some bridging
164 static void print_af_spec(FILE *fp
, struct rtattr
*attr
)
166 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+1];
168 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, attr
);
170 if (aftb
[IFLA_BRIDGE_MODE
])
171 print_hwmode(rta_getattr_u16(aftb
[IFLA_BRIDGE_MODE
]));
176 if (aftb
[IFLA_BRIDGE_VLAN_INFO
])
177 print_vlan_info(fp
, aftb
[IFLA_BRIDGE_VLAN_INFO
]);
180 int print_linkinfo(const struct sockaddr_nl
*who
,
181 struct nlmsghdr
*n
, void *arg
)
184 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
185 struct rtattr
*tb
[IFLA_MAX
+1];
186 unsigned int m_flag
= 0;
187 int len
= n
->nlmsg_len
;
190 len
-= NLMSG_LENGTH(sizeof(*ifi
));
192 fprintf(stderr
, "Message too short!\n");
196 if (!(ifi
->ifi_family
== AF_BRIDGE
|| ifi
->ifi_family
== AF_UNSPEC
))
199 if (filter_index
&& filter_index
!= ifi
->ifi_index
)
202 parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
, NLA_F_NESTED
);
204 name
= get_ifname_rta(ifi
->ifi_index
, tb
[IFLA_IFNAME
]);
208 open_json_object(NULL
);
209 if (n
->nlmsg_type
== RTM_DELLINK
)
210 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
212 print_int(PRINT_ANY
, "ifindex", "%d: ", ifi
->ifi_index
);
213 m_flag
= print_name_and_link("%s: ", COLOR_IFNAME
, name
, tb
);
214 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
219 rta_getattr_u32(tb
[IFLA_MTU
]));
221 if (tb
[IFLA_MASTER
]) {
222 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
224 print_string(PRINT_ANY
, "master", "master %s ",
225 ll_index_to_name(master
));
228 if (tb
[IFLA_PROTINFO
])
229 print_protinfo(fp
, tb
[IFLA_PROTINFO
]);
231 if (tb
[IFLA_AF_SPEC
])
232 print_af_spec(fp
, tb
[IFLA_AF_SPEC
]);
234 print_string(PRINT_FP
, NULL
, "%s", "\n");
240 static void usage(void)
242 fprintf(stderr
, "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n");
243 fprintf(stderr
, " [ guard {on | off} ]\n");
244 fprintf(stderr
, " [ hairpin {on | off} ]\n");
245 fprintf(stderr
, " [ fastleave {on | off} ]\n");
246 fprintf(stderr
, " [ root_block {on | off} ]\n");
247 fprintf(stderr
, " [ learning {on | off} ]\n");
248 fprintf(stderr
, " [ learning_sync {on | off} ]\n");
249 fprintf(stderr
, " [ flood {on | off} ]\n");
250 fprintf(stderr
, " [ mcast_flood {on | off} ]\n");
251 fprintf(stderr
, " [ neigh_suppress {on | off} ]\n");
252 fprintf(stderr
, " [ vlan_tunnel {on | off} ]\n");
253 fprintf(stderr
, " [ hwmode {vepa | veb} ]\n");
254 fprintf(stderr
, " [ self ] [ master ]\n");
255 fprintf(stderr
, " bridge link show [dev DEV]\n");
259 static bool on_off(char *arg
, __s8
*attr
, char *val
)
261 if (strcmp(val
, "on") == 0)
263 else if (strcmp(val
, "off") == 0)
267 "Error: argument of \"%s\" must be \"on\" or \"off\"\n",
275 static int brlink_modify(int argc
, char **argv
)
279 struct ifinfomsg ifm
;
282 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
283 .n
.nlmsg_flags
= NLM_F_REQUEST
,
284 .n
.nlmsg_type
= RTM_SETLINK
,
285 .ifm
.ifi_family
= PF_BRIDGE
,
288 __s8 neigh_suppress
= -1;
290 __s8 learning_sync
= -1;
292 __s8 vlan_tunnel
= -1;
293 __s8 mcast_flood
= -1;
295 __s8 bpdu_guard
= -1;
296 __s8 fast_leave
= -1;
297 __s8 root_block
= -1;
306 if (strcmp(*argv
, "dev") == 0) {
309 } else if (strcmp(*argv
, "guard") == 0) {
311 if (!on_off("guard", &bpdu_guard
, *argv
))
313 } else if (strcmp(*argv
, "hairpin") == 0) {
315 if (!on_off("hairping", &hairpin
, *argv
))
317 } else if (strcmp(*argv
, "fastleave") == 0) {
319 if (!on_off("fastleave", &fast_leave
, *argv
))
321 } else if (strcmp(*argv
, "root_block") == 0) {
323 if (!on_off("root_block", &root_block
, *argv
))
325 } else if (strcmp(*argv
, "learning") == 0) {
327 if (!on_off("learning", &learning
, *argv
))
329 } else if (strcmp(*argv
, "learning_sync") == 0) {
331 if (!on_off("learning_sync", &learning_sync
, *argv
))
333 } else if (strcmp(*argv
, "flood") == 0) {
335 if (!on_off("flood", &flood
, *argv
))
337 } else if (strcmp(*argv
, "mcast_flood") == 0) {
339 if (!on_off("mcast_flood", &mcast_flood
, *argv
))
341 } else if (strcmp(*argv
, "cost") == 0) {
344 } else if (strcmp(*argv
, "priority") == 0) {
346 priority
= atoi(*argv
);
347 } else if (strcmp(*argv
, "state") == 0) {
350 size_t nstates
= ARRAY_SIZE(port_states
);
352 state
= strtol(*argv
, &endptr
, 10);
353 if (!(**argv
!= '\0' && *endptr
== '\0')) {
354 for (state
= 0; state
< nstates
; state
++)
355 if (strcmp(port_states
[state
], *argv
) == 0)
357 if (state
== nstates
) {
359 "Error: invalid STP port state\n");
363 } else if (strcmp(*argv
, "hwmode") == 0) {
365 flags
= BRIDGE_FLAGS_SELF
;
366 if (strcmp(*argv
, "vepa") == 0)
367 mode
= BRIDGE_MODE_VEPA
;
368 else if (strcmp(*argv
, "veb") == 0)
369 mode
= BRIDGE_MODE_VEB
;
372 "Mode argument must be \"vepa\" or \"veb\".\n");
375 } else if (strcmp(*argv
, "self") == 0) {
376 flags
|= BRIDGE_FLAGS_SELF
;
377 } else if (strcmp(*argv
, "master") == 0) {
378 flags
|= BRIDGE_FLAGS_MASTER
;
379 } else if (strcmp(*argv
, "neigh_suppress") == 0) {
381 if (!on_off("neigh_suppress", &neigh_suppress
,
384 } else if (strcmp(*argv
, "vlan_tunnel") == 0) {
386 if (!on_off("vlan_tunnel", &vlan_tunnel
,
395 fprintf(stderr
, "Device is a required argument.\n");
400 req
.ifm
.ifi_index
= ll_name_to_index(d
);
401 if (req
.ifm
.ifi_index
== 0) {
402 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", d
);
406 /* Nested PROTINFO attribute. Contains: port flags, cost, priority and
409 nest
= addattr_nest(&req
.n
, sizeof(req
),
410 IFLA_PROTINFO
| NLA_F_NESTED
);
413 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_GUARD
, bpdu_guard
);
415 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_MODE
, hairpin
);
417 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_FAST_LEAVE
,
420 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_PROTECT
, root_block
);
422 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_UNICAST_FLOOD
, flood
);
423 if (mcast_flood
>= 0)
424 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_MCAST_FLOOD
,
427 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_LEARNING
, learning
);
428 if (learning_sync
>= 0)
429 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_LEARNING_SYNC
,
433 addattr32(&req
.n
, sizeof(req
), IFLA_BRPORT_COST
, cost
);
436 addattr16(&req
.n
, sizeof(req
), IFLA_BRPORT_PRIORITY
, priority
);
439 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_STATE
, state
);
441 if (neigh_suppress
!= -1)
442 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_NEIGH_SUPPRESS
,
444 if (vlan_tunnel
!= -1)
445 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_VLAN_TUNNEL
,
448 addattr_nest_end(&req
.n
, nest
);
450 /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that
451 * designates master or self operation and IFLA_BRIDGE_MODE
452 * for hw 'vepa' or 'veb' operation modes. The hwmodes are
453 * only valid in 'self' mode on some devices so far.
455 if (mode
>= 0 || flags
> 0) {
456 nest
= addattr_nest(&req
.n
, sizeof(req
), IFLA_AF_SPEC
);
459 addattr16(&req
.n
, sizeof(req
), IFLA_BRIDGE_FLAGS
, flags
);
462 addattr16(&req
.n
, sizeof(req
), IFLA_BRIDGE_MODE
, mode
);
464 addattr_nest_end(&req
.n
, nest
);
467 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
473 static int brlink_show(int argc
, char **argv
)
475 char *filter_dev
= NULL
;
478 if (strcmp(*argv
, "dev") == 0) {
481 duparg("dev", *argv
);
488 if ((filter_index
= ll_name_to_index(filter_dev
)) == 0) {
489 fprintf(stderr
, "Cannot find device \"%s\"\n",
496 if (rtnl_wilddump_req_filter(&rth
, PF_BRIDGE
, RTM_GETLINK
,
498 RTEXT_FILTER_BRVLAN_COMPRESSED
:
499 RTEXT_FILTER_BRVLAN
)) < 0) {
500 perror("Cannon send dump request");
504 if (rtnl_wilddump_request(&rth
, PF_BRIDGE
, RTM_GETLINK
) < 0) {
505 perror("Cannon send dump request");
511 if (rtnl_dump_filter(&rth
, print_linkinfo
, stdout
) < 0) {
512 fprintf(stderr
, "Dump terminated\n");
521 int do_link(int argc
, char **argv
)
525 if (matches(*argv
, "set") == 0 ||
526 matches(*argv
, "change") == 0)
527 return brlink_modify(argc
-1, argv
+1);
528 if (matches(*argv
, "show") == 0 ||
529 matches(*argv
, "lst") == 0 ||
530 matches(*argv
, "list") == 0)
531 return brlink_show(argc
-1, argv
+1);
532 if (matches(*argv
, "help") == 0)
535 return brlink_show(0, NULL
);
537 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv
);