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_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 if (prtb
[IFLA_BRPORT_BACKUP_PORT
]) {
159 ifidx
= rta_getattr_u32(prtb
[IFLA_BRPORT_BACKUP_PORT
]);
160 print_string(PRINT_ANY
,
161 "backup_port", "backup_port %s ",
162 ll_index_to_name(ifidx
));
165 if (prtb
[IFLA_BRPORT_ISOLATED
])
166 print_onoff(fp
, "isolated",
167 rta_getattr_u8(prtb
[IFLA_BRPORT_ISOLATED
]));
169 print_portstate(rta_getattr_u8(attr
));
174 * This is reported by HW devices that have some bridging
177 static void print_af_spec(struct rtattr
*attr
, int ifindex
)
179 struct rtattr
*aftb
[IFLA_BRIDGE_MAX
+1];
181 parse_rtattr_nested(aftb
, IFLA_BRIDGE_MAX
, attr
);
183 if (aftb
[IFLA_BRIDGE_MODE
])
184 print_hwmode(rta_getattr_u16(aftb
[IFLA_BRIDGE_MODE
]));
189 if (aftb
[IFLA_BRIDGE_VLAN_INFO
])
190 print_vlan_info(aftb
[IFLA_BRIDGE_VLAN_INFO
], ifindex
);
193 int print_linkinfo(struct nlmsghdr
*n
, void *arg
)
196 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
197 struct rtattr
*tb
[IFLA_MAX
+1];
198 unsigned int m_flag
= 0;
199 int len
= n
->nlmsg_len
;
202 len
-= NLMSG_LENGTH(sizeof(*ifi
));
204 fprintf(stderr
, "Message too short!\n");
208 if (!(ifi
->ifi_family
== AF_BRIDGE
|| ifi
->ifi_family
== AF_UNSPEC
))
211 if (filter_index
&& filter_index
!= ifi
->ifi_index
)
214 parse_rtattr_flags(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
, NLA_F_NESTED
);
216 name
= get_ifname_rta(ifi
->ifi_index
, tb
[IFLA_IFNAME
]);
220 open_json_object(NULL
);
221 if (n
->nlmsg_type
== RTM_DELLINK
)
222 print_bool(PRINT_ANY
, "deleted", "Deleted ", true);
224 print_int(PRINT_ANY
, "ifindex", "%d: ", ifi
->ifi_index
);
225 m_flag
= print_name_and_link("%s: ", name
, tb
);
226 print_link_flags(fp
, ifi
->ifi_flags
, m_flag
);
231 rta_getattr_u32(tb
[IFLA_MTU
]));
233 if (tb
[IFLA_MASTER
]) {
234 int master
= rta_getattr_u32(tb
[IFLA_MASTER
]);
236 print_string(PRINT_ANY
, "master", "master %s ",
237 ll_index_to_name(master
));
240 if (tb
[IFLA_PROTINFO
])
241 print_protinfo(fp
, tb
[IFLA_PROTINFO
]);
243 if (tb
[IFLA_AF_SPEC
])
244 print_af_spec(tb
[IFLA_AF_SPEC
], ifi
->ifi_index
);
246 print_string(PRINT_FP
, NULL
, "%s", "\n");
252 static void usage(void)
254 fprintf(stderr
, "Usage: bridge link set dev DEV [ cost COST ] [ priority PRIO ] [ state STATE ]\n");
255 fprintf(stderr
, " [ guard {on | off} ]\n");
256 fprintf(stderr
, " [ hairpin {on | off} ]\n");
257 fprintf(stderr
, " [ fastleave {on | off} ]\n");
258 fprintf(stderr
, " [ root_block {on | off} ]\n");
259 fprintf(stderr
, " [ learning {on | off} ]\n");
260 fprintf(stderr
, " [ learning_sync {on | off} ]\n");
261 fprintf(stderr
, " [ flood {on | off} ]\n");
262 fprintf(stderr
, " [ mcast_flood {on | off} ]\n");
263 fprintf(stderr
, " [ neigh_suppress {on | off} ]\n");
264 fprintf(stderr
, " [ vlan_tunnel {on | off} ]\n");
265 fprintf(stderr
, " [ isolated {on | off} ]\n");
266 fprintf(stderr
, " [ hwmode {vepa | veb} ]\n");
267 fprintf(stderr
, " [ backup_port DEVICE ] [ nobackup_port ]\n");
268 fprintf(stderr
, " [ self ] [ master ]\n");
269 fprintf(stderr
, " bridge link show [dev DEV]\n");
273 static bool on_off(char *arg
, __s8
*attr
, char *val
)
275 if (strcmp(val
, "on") == 0)
277 else if (strcmp(val
, "off") == 0)
281 "Error: argument of \"%s\" must be \"on\" or \"off\"\n",
289 static int brlink_modify(int argc
, char **argv
)
293 struct ifinfomsg ifm
;
296 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
297 .n
.nlmsg_flags
= NLM_F_REQUEST
,
298 .n
.nlmsg_type
= RTM_SETLINK
,
299 .ifm
.ifi_family
= PF_BRIDGE
,
302 int backup_port_idx
= -1;
303 __s8 neigh_suppress
= -1;
305 __s8 learning_sync
= -1;
307 __s8 vlan_tunnel
= -1;
308 __s8 mcast_flood
= -1;
311 __s8 bpdu_guard
= -1;
312 __s8 fast_leave
= -1;
313 __s8 root_block
= -1;
322 if (strcmp(*argv
, "dev") == 0) {
325 } else if (strcmp(*argv
, "guard") == 0) {
327 if (!on_off("guard", &bpdu_guard
, *argv
))
329 } else if (strcmp(*argv
, "hairpin") == 0) {
331 if (!on_off("hairpin", &hairpin
, *argv
))
333 } else if (strcmp(*argv
, "fastleave") == 0) {
335 if (!on_off("fastleave", &fast_leave
, *argv
))
337 } else if (strcmp(*argv
, "root_block") == 0) {
339 if (!on_off("root_block", &root_block
, *argv
))
341 } else if (strcmp(*argv
, "learning") == 0) {
343 if (!on_off("learning", &learning
, *argv
))
345 } else if (strcmp(*argv
, "learning_sync") == 0) {
347 if (!on_off("learning_sync", &learning_sync
, *argv
))
349 } else if (strcmp(*argv
, "flood") == 0) {
351 if (!on_off("flood", &flood
, *argv
))
353 } else if (strcmp(*argv
, "mcast_flood") == 0) {
355 if (!on_off("mcast_flood", &mcast_flood
, *argv
))
357 } else if (strcmp(*argv
, "cost") == 0) {
360 } else if (strcmp(*argv
, "priority") == 0) {
362 priority
= atoi(*argv
);
363 } else if (strcmp(*argv
, "state") == 0) {
366 size_t nstates
= ARRAY_SIZE(port_states
);
368 state
= strtol(*argv
, &endptr
, 10);
369 if (!(**argv
!= '\0' && *endptr
== '\0')) {
370 for (state
= 0; state
< nstates
; state
++)
371 if (strcmp(port_states
[state
], *argv
) == 0)
373 if (state
== nstates
) {
375 "Error: invalid STP port state\n");
379 } else if (strcmp(*argv
, "hwmode") == 0) {
381 flags
= BRIDGE_FLAGS_SELF
;
382 if (strcmp(*argv
, "vepa") == 0)
383 mode
= BRIDGE_MODE_VEPA
;
384 else if (strcmp(*argv
, "veb") == 0)
385 mode
= BRIDGE_MODE_VEB
;
388 "Mode argument must be \"vepa\" or \"veb\".\n");
391 } else if (strcmp(*argv
, "self") == 0) {
392 flags
|= BRIDGE_FLAGS_SELF
;
393 } else if (strcmp(*argv
, "master") == 0) {
394 flags
|= BRIDGE_FLAGS_MASTER
;
395 } else if (strcmp(*argv
, "neigh_suppress") == 0) {
397 if (!on_off("neigh_suppress", &neigh_suppress
,
400 } else if (strcmp(*argv
, "vlan_tunnel") == 0) {
402 if (!on_off("vlan_tunnel", &vlan_tunnel
,
405 } else if (strcmp(*argv
, "isolated") == 0) {
407 if (!on_off("isolated", &isolated
, *argv
))
409 } else if (strcmp(*argv
, "backup_port") == 0) {
411 backup_port_idx
= ll_name_to_index(*argv
);
412 if (!backup_port_idx
) {
413 fprintf(stderr
, "Error: device %s does not exist\n",
417 } else if (strcmp(*argv
, "nobackup_port") == 0) {
425 fprintf(stderr
, "Device is a required argument.\n");
430 req
.ifm
.ifi_index
= ll_name_to_index(d
);
431 if (req
.ifm
.ifi_index
== 0) {
432 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", d
);
436 /* Nested PROTINFO attribute. Contains: port flags, cost, priority and
439 nest
= addattr_nest(&req
.n
, sizeof(req
),
440 IFLA_PROTINFO
| NLA_F_NESTED
);
443 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_GUARD
, bpdu_guard
);
445 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_MODE
, hairpin
);
447 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_FAST_LEAVE
,
450 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_PROTECT
, root_block
);
452 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_UNICAST_FLOOD
, flood
);
453 if (mcast_flood
>= 0)
454 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_MCAST_FLOOD
,
457 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_LEARNING
, learning
);
458 if (learning_sync
>= 0)
459 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_LEARNING_SYNC
,
463 addattr32(&req
.n
, sizeof(req
), IFLA_BRPORT_COST
, cost
);
466 addattr16(&req
.n
, sizeof(req
), IFLA_BRPORT_PRIORITY
, priority
);
469 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_STATE
, state
);
471 if (neigh_suppress
!= -1)
472 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_NEIGH_SUPPRESS
,
474 if (vlan_tunnel
!= -1)
475 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_VLAN_TUNNEL
,
478 addattr8(&req
.n
, sizeof(req
), IFLA_BRPORT_ISOLATED
, isolated
);
480 if (backup_port_idx
!= -1)
481 addattr32(&req
.n
, sizeof(req
), IFLA_BRPORT_BACKUP_PORT
,
484 addattr_nest_end(&req
.n
, nest
);
486 /* IFLA_AF_SPEC nested attribute. Contains IFLA_BRIDGE_FLAGS that
487 * designates master or self operation and IFLA_BRIDGE_MODE
488 * for hw 'vepa' or 'veb' operation modes. The hwmodes are
489 * only valid in 'self' mode on some devices so far.
491 if (mode
>= 0 || flags
> 0) {
492 nest
= addattr_nest(&req
.n
, sizeof(req
), IFLA_AF_SPEC
);
495 addattr16(&req
.n
, sizeof(req
), IFLA_BRIDGE_FLAGS
, flags
);
498 addattr16(&req
.n
, sizeof(req
), IFLA_BRIDGE_MODE
, mode
);
500 addattr_nest_end(&req
.n
, nest
);
503 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
509 static int brlink_show(int argc
, char **argv
)
511 char *filter_dev
= NULL
;
514 if (strcmp(*argv
, "dev") == 0) {
517 duparg("dev", *argv
);
524 filter_index
= ll_name_to_index(filter_dev
);
526 return nodev(filter_dev
);
530 if (rtnl_linkdump_req_filter(&rth
, PF_BRIDGE
,
532 RTEXT_FILTER_BRVLAN_COMPRESSED
:
533 RTEXT_FILTER_BRVLAN
)) < 0) {
534 perror("Cannon send dump request");
538 if (rtnl_linkdump_req(&rth
, PF_BRIDGE
) < 0) {
539 perror("Cannon send dump request");
545 if (rtnl_dump_filter(&rth
, print_linkinfo
, stdout
) < 0) {
546 fprintf(stderr
, "Dump terminated\n");
555 int do_link(int argc
, char **argv
)
559 if (matches(*argv
, "set") == 0 ||
560 matches(*argv
, "change") == 0)
561 return brlink_modify(argc
-1, argv
+1);
562 if (matches(*argv
, "show") == 0 ||
563 matches(*argv
, "lst") == 0 ||
564 matches(*argv
, "list") == 0)
565 return brlink_show(argc
-1, argv
+1);
566 if (matches(*argv
, "help") == 0)
569 return brlink_show(0, NULL
);
571 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge link help\".\n", *argv
);