1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <linux/if_bridge.h>
10 #include <linux/if_ether.h>
13 #include "json_print.h"
14 #include "libnetlink.h"
15 #include "br_common.h"
18 static unsigned int filter_index
, filter_vlan
;
19 static int show_vlan_tunnel_info
= 0;
21 static void usage(void)
24 "Usage: bridge vlan { add | del } vid VLAN_ID dev DEV [ tunnel_info id TUNNEL_ID ]\n"
25 " [ pvid ] [ untagged ]\n"
26 " [ self ] [ master ]\n"
27 " bridge vlan { show } [ dev DEV ] [ vid VLAN_ID ]\n"
28 " bridge vlan { tunnelshow } [ dev DEV ] [ vid VLAN_ID ]\n");
32 static int parse_tunnel_info(int *argcp
, char ***argvp
, __u32
*tun_id_start
,
40 if (!matches(*argv
, "id")) {
42 t
= strchr(*argv
, '-');
45 if (get_u32(tun_id_start
, *argv
, 0) ||
46 *tun_id_start
>= 1u << 24)
47 invarg("invalid tun id", *argv
);
48 if (get_u32(tun_id_end
, t
+ 1, 0) ||
49 *tun_id_end
>= 1u << 24)
50 invarg("invalid tun id", *argv
);
53 if (get_u32(tun_id_start
, *argv
, 0) ||
54 *tun_id_start
>= 1u << 24)
55 invarg("invalid tun id", *argv
);
58 invarg("tunnel id expected", *argv
);
67 static int add_tunnel_info(struct nlmsghdr
*n
, int reqsize
,
68 __u16 vid
, __u32 tun_id
, __u16 flags
)
72 tinfo
= addattr_nest(n
, reqsize
, IFLA_BRIDGE_VLAN_TUNNEL_INFO
);
73 addattr32(n
, reqsize
, IFLA_BRIDGE_VLAN_TUNNEL_ID
, tun_id
);
74 addattr32(n
, reqsize
, IFLA_BRIDGE_VLAN_TUNNEL_VID
, vid
);
75 addattr32(n
, reqsize
, IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
, flags
);
77 addattr_nest_end(n
, tinfo
);
82 static int add_tunnel_info_range(struct nlmsghdr
*n
, int reqsize
,
83 __u16 vid_start
, int16_t vid_end
,
84 __u32 tun_id_start
, __u32 tun_id_end
)
86 if (vid_end
!= -1 && (vid_end
- vid_start
) > 0) {
87 add_tunnel_info(n
, reqsize
, vid_start
, tun_id_start
,
88 BRIDGE_VLAN_INFO_RANGE_BEGIN
);
90 add_tunnel_info(n
, reqsize
, vid_end
, tun_id_end
,
91 BRIDGE_VLAN_INFO_RANGE_END
);
93 add_tunnel_info(n
, reqsize
, vid_start
, tun_id_start
, 0);
99 static int add_vlan_info_range(struct nlmsghdr
*n
, int reqsize
, __u16 vid_start
,
100 int16_t vid_end
, __u16 flags
)
102 struct bridge_vlan_info vinfo
= {};
105 vinfo
.vid
= vid_start
;
107 /* send vlan range start */
108 addattr_l(n
, reqsize
, IFLA_BRIDGE_VLAN_INFO
, &vinfo
,
110 vinfo
.flags
&= ~BRIDGE_VLAN_INFO_RANGE_BEGIN
;
112 /* Now send the vlan range end */
113 vinfo
.flags
|= BRIDGE_VLAN_INFO_RANGE_END
;
115 addattr_l(n
, reqsize
, IFLA_BRIDGE_VLAN_INFO
, &vinfo
,
118 addattr_l(n
, reqsize
, IFLA_BRIDGE_VLAN_INFO
, &vinfo
,
125 static int vlan_modify(int cmd
, int argc
, char **argv
)
129 struct ifinfomsg ifm
;
132 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
133 .n
.nlmsg_flags
= NLM_F_REQUEST
,
135 .ifm
.ifi_family
= PF_BRIDGE
,
140 struct rtattr
*afspec
;
141 struct bridge_vlan_info vinfo
= {};
142 bool tunnel_info_set
= false;
143 unsigned short flags
= 0;
144 __u32 tun_id_start
= 0;
145 __u32 tun_id_end
= 0;
148 if (strcmp(*argv
, "dev") == 0) {
151 } else if (strcmp(*argv
, "vid") == 0) {
155 p
= strchr(*argv
, '-');
161 vinfo
.flags
|= BRIDGE_VLAN_INFO_RANGE_BEGIN
;
165 } else if (strcmp(*argv
, "self") == 0) {
166 flags
|= BRIDGE_FLAGS_SELF
;
167 } else if (strcmp(*argv
, "master") == 0) {
168 flags
|= BRIDGE_FLAGS_MASTER
;
169 } else if (strcmp(*argv
, "pvid") == 0) {
170 vinfo
.flags
|= BRIDGE_VLAN_INFO_PVID
;
171 } else if (strcmp(*argv
, "untagged") == 0) {
172 vinfo
.flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
173 } else if (strcmp(*argv
, "tunnel_info") == 0) {
174 if (parse_tunnel_info(&argc
, &argv
,
178 tunnel_info_set
= true;
180 if (matches(*argv
, "help") == 0)
186 if (d
== NULL
|| vid
== -1) {
187 fprintf(stderr
, "Device and VLAN ID are required arguments.\n");
191 req
.ifm
.ifi_index
= ll_name_to_index(d
);
192 if (req
.ifm
.ifi_index
== 0) {
193 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", d
);
198 fprintf(stderr
, "Invalid VLAN ID \"%hu\"\n", vid
);
202 if (vinfo
.flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
203 if (vid_end
== -1 || vid_end
>= 4096 || vid
>= vid_end
) {
204 fprintf(stderr
, "Invalid VLAN range \"%hu-%hu\"\n",
208 if (vinfo
.flags
& BRIDGE_VLAN_INFO_PVID
) {
210 "pvid cannot be configured for a vlan range\n");
215 afspec
= addattr_nest(&req
.n
, sizeof(req
), IFLA_AF_SPEC
);
218 addattr16(&req
.n
, sizeof(req
), IFLA_BRIDGE_FLAGS
, flags
);
221 add_tunnel_info_range(&req
.n
, sizeof(req
), vid
, vid_end
,
222 tun_id_start
, tun_id_end
);
224 add_vlan_info_range(&req
.n
, sizeof(req
), vid
, vid_end
,
227 addattr_nest_end(&req
.n
, afspec
);
229 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
235 /* In order to use this function for both filtering and non-filtering cases
236 * we need to make it a tristate:
237 * return -1 - if filtering we've gone over so don't continue
238 * return 0 - skip entry and continue (applies to range start or to entries
239 * which are less than filter_vlan)
240 * return 1 - print the entry and continue
242 static int filter_vlan_check(__u16 vid
, __u16 flags
)
244 /* if we're filtering we should stop on the first greater entry */
245 if (filter_vlan
&& vid
> filter_vlan
&&
246 !(flags
& BRIDGE_VLAN_INFO_RANGE_END
))
248 if ((flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) ||
255 static void open_vlan_port(int ifi_index
)
257 open_json_object(NULL
);
258 print_string(PRINT_ANY
, "ifname", "%s",
259 ll_index_to_name(ifi_index
));
260 open_json_array(PRINT_JSON
, "vlans");
263 static void close_vlan_port(void)
265 close_json_array(PRINT_JSON
, NULL
);
269 static void print_range(const char *name
, __u16 start
, __u16 id
)
273 snprintf(end
, sizeof(end
), "%sEnd", name
);
275 print_hu(PRINT_ANY
, name
, "\t %hu", start
);
277 print_hu(PRINT_ANY
, end
, "-%hu", id
);
281 static void print_vlan_tunnel_info(FILE *fp
, struct rtattr
*tb
, int ifindex
)
283 struct rtattr
*i
, *list
= tb
;
284 int rem
= RTA_PAYLOAD(list
);
285 __u16 last_vid_start
= 0;
286 __u32 last_tunid_start
= 0;
289 open_vlan_port(ifindex
);
291 open_json_array(PRINT_JSON
, "tunnel");
292 for (i
= RTA_DATA(list
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
293 struct rtattr
*ttb
[IFLA_BRIDGE_VLAN_TUNNEL_MAX
+1];
295 __u16 tunnel_vid
= 0;
296 __u16 tunnel_flags
= 0;
299 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_TUNNEL_INFO
)
302 parse_rtattr(ttb
, IFLA_BRIDGE_VLAN_TUNNEL_MAX
,
303 RTA_DATA(i
), RTA_PAYLOAD(i
));
305 if (ttb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
])
307 rta_getattr_u32(ttb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
]);
311 if (ttb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
])
313 rta_getattr_u32(ttb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
]);
315 if (ttb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
])
317 rta_getattr_u32(ttb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
]);
319 if (!(tunnel_flags
& BRIDGE_VLAN_INFO_RANGE_END
)) {
320 last_vid_start
= tunnel_vid
;
321 last_tunid_start
= tunnel_id
;
324 vcheck_ret
= filter_vlan_check(tunnel_vid
, tunnel_flags
);
325 if (vcheck_ret
== -1)
327 else if (vcheck_ret
== 0)
330 if (tunnel_flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
)
334 open_vlan_port(ifindex
);
336 open_json_object(NULL
);
337 print_range("vlan", last_vid_start
, tunnel_vid
);
338 print_range("tunid", last_tunid_start
, tunnel_id
);
341 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
350 static int print_vlan_tunnel(const struct sockaddr_nl
*who
,
354 struct ifinfomsg
*ifm
= NLMSG_DATA(n
);
355 struct rtattr
*tb
[IFLA_MAX
+1];
356 int len
= n
->nlmsg_len
;
359 if (n
->nlmsg_type
!= RTM_NEWLINK
) {
360 fprintf(stderr
, "Not RTM_NEWLINK: %08x %08x %08x\n",
361 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
365 len
-= NLMSG_LENGTH(sizeof(*ifm
));
367 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
371 if (ifm
->ifi_family
!= AF_BRIDGE
)
374 if (filter_index
&& filter_index
!= ifm
->ifi_index
)
377 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifm
), len
);
379 /* if AF_SPEC isn't there, vlan table is not preset for this port */
380 if (!tb
[IFLA_AF_SPEC
]) {
381 if (!filter_vlan
&& !is_json_context()) {
382 color_fprintf(fp
, COLOR_IFNAME
, "%s",
383 ll_index_to_name(ifm
->ifi_index
));
384 fprintf(fp
, "\tNone\n");
389 print_vlan_tunnel_info(fp
, tb
[IFLA_AF_SPEC
], ifm
->ifi_index
);
395 static int print_vlan(const struct sockaddr_nl
*who
,
400 struct ifinfomsg
*ifm
= NLMSG_DATA(n
);
401 int len
= n
->nlmsg_len
;
402 struct rtattr
*tb
[IFLA_MAX
+1];
404 if (n
->nlmsg_type
!= RTM_NEWLINK
) {
405 fprintf(stderr
, "Not RTM_NEWLINK: %08x %08x %08x\n",
406 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
410 len
-= NLMSG_LENGTH(sizeof(*ifm
));
412 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
416 if (ifm
->ifi_family
!= AF_BRIDGE
)
419 if (filter_index
&& filter_index
!= ifm
->ifi_index
)
422 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifm
), len
);
424 /* if AF_SPEC isn't there, vlan table is not preset for this port */
425 if (!tb
[IFLA_AF_SPEC
]) {
426 if (!filter_vlan
&& !is_json_context()) {
427 color_fprintf(fp
, COLOR_IFNAME
, "%s",
428 ll_index_to_name(ifm
->ifi_index
));
429 fprintf(fp
, "\tNone\n");
434 print_vlan_info(tb
[IFLA_AF_SPEC
], ifm
->ifi_index
);
435 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
441 static void print_vlan_flags(__u16 flags
)
446 open_json_array(PRINT_JSON
, "flags");
447 if (flags
& BRIDGE_VLAN_INFO_PVID
)
448 print_string(PRINT_ANY
, NULL
, " %s", "PVID");
450 if (flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
451 print_string(PRINT_ANY
, NULL
, " %s", "Egress Untagged");
452 close_json_array(PRINT_JSON
, NULL
);
455 static void print_one_vlan_stats(const struct bridge_vlan_xstats
*vstats
)
457 open_json_object(NULL
);
458 print_hu(PRINT_ANY
, "vid", " %hu", vstats
->vid
);
460 print_vlan_flags(vstats
->flags
);
462 print_lluint(PRINT_ANY
, "rx_bytes",
465 print_lluint(PRINT_ANY
, "rx_packets", " %llu packets\n",
467 print_lluint(PRINT_ANY
, "tx_bytes",
470 print_lluint(PRINT_ANY
, "tx_packets", " %llu packets",
475 static void print_vlan_stats_attr(struct rtattr
*attr
, int ifindex
)
477 struct rtattr
*brtb
[LINK_XSTATS_TYPE_MAX
+1];
478 struct rtattr
*i
, *list
;
482 parse_rtattr(brtb
, LINK_XSTATS_TYPE_MAX
, RTA_DATA(attr
),
484 if (!brtb
[LINK_XSTATS_TYPE_BRIDGE
])
487 list
= brtb
[LINK_XSTATS_TYPE_BRIDGE
];
488 rem
= RTA_PAYLOAD(list
);
490 ifname
= ll_index_to_name(ifindex
);
491 open_json_object(ifname
);
493 print_color_string(PRINT_FP
, COLOR_IFNAME
,
494 NULL
, "%-16s", ifname
);
496 for (i
= RTA_DATA(list
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
497 const struct bridge_vlan_xstats
*vstats
= RTA_DATA(i
);
499 if (i
->rta_type
!= BRIDGE_XSTATS_VLAN
)
502 if (filter_vlan
&& filter_vlan
!= vstats
->vid
)
505 /* skip pure port entries, they'll be dumped via the slave stats call */
506 if ((vstats
->flags
& BRIDGE_VLAN_INFO_MASTER
) &&
507 !(vstats
->flags
& BRIDGE_VLAN_INFO_BRENTRY
))
510 print_one_vlan_stats(vstats
);
516 static int print_vlan_stats(const struct sockaddr_nl
*who
,
520 struct if_stats_msg
*ifsm
= NLMSG_DATA(n
);
521 struct rtattr
*tb
[IFLA_STATS_MAX
+1];
522 int len
= n
->nlmsg_len
;
525 len
-= NLMSG_LENGTH(sizeof(*ifsm
));
527 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
531 if (filter_index
&& filter_index
!= ifsm
->ifindex
)
534 parse_rtattr(tb
, IFLA_STATS_MAX
, IFLA_STATS_RTA(ifsm
), len
);
536 /* We have to check if any of the two attrs are usable */
537 if (tb
[IFLA_STATS_LINK_XSTATS
])
538 print_vlan_stats_attr(tb
[IFLA_STATS_LINK_XSTATS
],
541 if (tb
[IFLA_STATS_LINK_XSTATS_SLAVE
])
542 print_vlan_stats_attr(tb
[IFLA_STATS_LINK_XSTATS_SLAVE
],
549 static int vlan_show(int argc
, char **argv
)
551 char *filter_dev
= NULL
;
555 if (strcmp(*argv
, "dev") == 0) {
558 duparg("dev", *argv
);
560 } else if (strcmp(*argv
, "vid") == 0) {
563 duparg("vid", *argv
);
564 filter_vlan
= atoi(*argv
);
570 filter_index
= ll_name_to_index(filter_dev
);
572 return nodev(filter_dev
);
578 if (rtnl_wilddump_req_filter(&rth
, PF_BRIDGE
, RTM_GETLINK
,
580 RTEXT_FILTER_BRVLAN_COMPRESSED
:
581 RTEXT_FILTER_BRVLAN
)) < 0) {
582 perror("Cannont send dump request");
586 if (!is_json_context()) {
587 if (show_vlan_tunnel_info
)
588 printf("port\tvlan ids\ttunnel id\n");
590 printf("port\tvlan ids\n");
593 if (show_vlan_tunnel_info
)
594 ret
= rtnl_dump_filter(&rth
, print_vlan_tunnel
,
597 ret
= rtnl_dump_filter(&rth
, print_vlan
, stdout
);
599 fprintf(stderr
, "Dump ternminated\n");
605 filt_mask
= IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS
);
606 if (rtnl_wilddump_stats_req_filter(&rth
, AF_UNSPEC
,
609 perror("Cannont send dump request");
613 if (!is_json_context())
614 printf("%-16s vlan id\n", "port");
616 if (rtnl_dump_filter(&rth
, print_vlan_stats
, stdout
) < 0) {
617 fprintf(stderr
, "Dump terminated\n");
621 filt_mask
= IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS_SLAVE
);
622 if (rtnl_wilddump_stats_req_filter(&rth
, AF_UNSPEC
,
625 perror("Cannont send slave dump request");
629 if (rtnl_dump_filter(&rth
, print_vlan_stats
, stdout
) < 0) {
630 fprintf(stderr
, "Dump terminated\n");
640 void print_vlan_info(struct rtattr
*tb
, int ifindex
)
642 struct rtattr
*i
, *list
= tb
;
643 int rem
= RTA_PAYLOAD(list
);
644 __u16 last_vid_start
= 0;
646 open_vlan_port(ifindex
);
648 for (i
= RTA_DATA(list
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
649 struct bridge_vlan_info
*vinfo
;
652 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_INFO
)
657 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_END
))
658 last_vid_start
= vinfo
->vid
;
659 vcheck_ret
= filter_vlan_check(vinfo
->vid
, vinfo
->flags
);
660 if (vcheck_ret
== -1)
662 else if (vcheck_ret
== 0)
665 open_json_object(NULL
);
666 print_range("vlan", last_vid_start
, vinfo
->vid
);
668 print_vlan_flags(vinfo
->flags
);
670 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
675 int do_vlan(int argc
, char **argv
)
680 if (matches(*argv
, "add") == 0)
681 return vlan_modify(RTM_SETLINK
, argc
-1, argv
+1);
682 if (matches(*argv
, "delete") == 0)
683 return vlan_modify(RTM_DELLINK
, argc
-1, argv
+1);
684 if (matches(*argv
, "show") == 0 ||
685 matches(*argv
, "lst") == 0 ||
686 matches(*argv
, "list") == 0)
687 return vlan_show(argc
-1, argv
+1);
688 if (matches(*argv
, "tunnelshow") == 0) {
689 show_vlan_tunnel_info
= 1;
690 return vlan_show(argc
-1, argv
+1);
692 if (matches(*argv
, "help") == 0)
695 return vlan_show(0, NULL
);
698 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge vlan help\".\n", *argv
);