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>
11 #include <json_writer.h>
14 #include "libnetlink.h"
15 #include "br_common.h"
18 static unsigned int filter_index
, filter_vlan
;
19 static int last_ifidx
= -1;
20 static int show_vlan_tunnel_info
= 0;
22 json_writer_t
*jw_global
;
24 static void usage(void)
27 "Usage: bridge vlan { add | del } vid VLAN_ID dev DEV [ tunnel_info id TUNNEL_ID ]\n"
28 " [ pvid ] [ untagged ]\n"
29 " [ self ] [ master ]\n"
30 " bridge vlan { show } [ dev DEV ] [ vid VLAN_ID ]\n"
31 " bridge vlan { tunnelshow } [ dev DEV ] [ vid VLAN_ID ]\n");
35 static int parse_tunnel_info(int *argcp
, char ***argvp
, __u32
*tun_id_start
,
43 if (!matches(*argv
, "id")) {
45 t
= strchr(*argv
, '-');
48 if (get_u32(tun_id_start
, *argv
, 0) ||
49 *tun_id_start
>= 1u << 24)
50 invarg("invalid tun id", *argv
);
51 if (get_u32(tun_id_end
, t
+ 1, 0) ||
52 *tun_id_end
>= 1u << 24)
53 invarg("invalid tun id", *argv
);
56 if (get_u32(tun_id_start
, *argv
, 0) ||
57 *tun_id_start
>= 1u << 24)
58 invarg("invalid tun id", *argv
);
61 invarg("tunnel id expected", *argv
);
70 static int add_tunnel_info(struct nlmsghdr
*n
, int reqsize
,
71 __u16 vid
, __u32 tun_id
, __u16 flags
)
75 tinfo
= addattr_nest(n
, reqsize
, IFLA_BRIDGE_VLAN_TUNNEL_INFO
);
76 addattr32(n
, reqsize
, IFLA_BRIDGE_VLAN_TUNNEL_ID
, tun_id
);
77 addattr32(n
, reqsize
, IFLA_BRIDGE_VLAN_TUNNEL_VID
, vid
);
78 addattr32(n
, reqsize
, IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
, flags
);
80 addattr_nest_end(n
, tinfo
);
85 static int add_tunnel_info_range(struct nlmsghdr
*n
, int reqsize
,
86 __u16 vid_start
, int16_t vid_end
,
87 __u32 tun_id_start
, __u32 tun_id_end
)
89 if (vid_end
!= -1 && (vid_end
- vid_start
) > 0) {
90 add_tunnel_info(n
, reqsize
, vid_start
, tun_id_start
,
91 BRIDGE_VLAN_INFO_RANGE_BEGIN
);
93 add_tunnel_info(n
, reqsize
, vid_end
, tun_id_end
,
94 BRIDGE_VLAN_INFO_RANGE_END
);
96 add_tunnel_info(n
, reqsize
, vid_start
, tun_id_start
, 0);
102 static int add_vlan_info_range(struct nlmsghdr
*n
, int reqsize
, __u16 vid_start
,
103 int16_t vid_end
, __u16 flags
)
105 struct bridge_vlan_info vinfo
= {};
108 vinfo
.vid
= vid_start
;
110 /* send vlan range start */
111 addattr_l(n
, reqsize
, IFLA_BRIDGE_VLAN_INFO
, &vinfo
,
113 vinfo
.flags
&= ~BRIDGE_VLAN_INFO_RANGE_BEGIN
;
115 /* Now send the vlan range end */
116 vinfo
.flags
|= BRIDGE_VLAN_INFO_RANGE_END
;
118 addattr_l(n
, reqsize
, IFLA_BRIDGE_VLAN_INFO
, &vinfo
,
121 addattr_l(n
, reqsize
, IFLA_BRIDGE_VLAN_INFO
, &vinfo
,
128 static int vlan_modify(int cmd
, int argc
, char **argv
)
132 struct ifinfomsg ifm
;
135 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
136 .n
.nlmsg_flags
= NLM_F_REQUEST
,
138 .ifm
.ifi_family
= PF_BRIDGE
,
143 struct rtattr
*afspec
;
144 struct bridge_vlan_info vinfo
= {};
145 bool tunnel_info_set
= false;
146 unsigned short flags
= 0;
147 __u32 tun_id_start
= 0;
148 __u32 tun_id_end
= 0;
151 if (strcmp(*argv
, "dev") == 0) {
154 } else if (strcmp(*argv
, "vid") == 0) {
158 p
= strchr(*argv
, '-');
164 vinfo
.flags
|= BRIDGE_VLAN_INFO_RANGE_BEGIN
;
168 } else if (strcmp(*argv
, "self") == 0) {
169 flags
|= BRIDGE_FLAGS_SELF
;
170 } else if (strcmp(*argv
, "master") == 0) {
171 flags
|= BRIDGE_FLAGS_MASTER
;
172 } else if (strcmp(*argv
, "pvid") == 0) {
173 vinfo
.flags
|= BRIDGE_VLAN_INFO_PVID
;
174 } else if (strcmp(*argv
, "untagged") == 0) {
175 vinfo
.flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
176 } else if (strcmp(*argv
, "tunnel_info") == 0) {
177 if (parse_tunnel_info(&argc
, &argv
,
181 tunnel_info_set
= true;
183 if (matches(*argv
, "help") == 0)
189 if (d
== NULL
|| vid
== -1) {
190 fprintf(stderr
, "Device and VLAN ID are required arguments.\n");
194 req
.ifm
.ifi_index
= ll_name_to_index(d
);
195 if (req
.ifm
.ifi_index
== 0) {
196 fprintf(stderr
, "Cannot find bridge device \"%s\"\n", d
);
201 fprintf(stderr
, "Invalid VLAN ID \"%hu\"\n", vid
);
205 if (vinfo
.flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
206 if (vid_end
== -1 || vid_end
>= 4096 || vid
>= vid_end
) {
207 fprintf(stderr
, "Invalid VLAN range \"%hu-%hu\"\n",
211 if (vinfo
.flags
& BRIDGE_VLAN_INFO_PVID
) {
213 "pvid cannot be configured for a vlan range\n");
218 afspec
= addattr_nest(&req
.n
, sizeof(req
), IFLA_AF_SPEC
);
221 addattr16(&req
.n
, sizeof(req
), IFLA_BRIDGE_FLAGS
, flags
);
224 add_tunnel_info_range(&req
.n
, sizeof(req
), vid
, vid_end
,
225 tun_id_start
, tun_id_end
);
227 add_vlan_info_range(&req
.n
, sizeof(req
), vid
, vid_end
,
230 addattr_nest_end(&req
.n
, afspec
);
232 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
238 /* In order to use this function for both filtering and non-filtering cases
239 * we need to make it a tristate:
240 * return -1 - if filtering we've gone over so don't continue
241 * return 0 - skip entry and continue (applies to range start or to entries
242 * which are less than filter_vlan)
243 * return 1 - print the entry and continue
245 static int filter_vlan_check(__u16 vid
, __u16 flags
)
247 /* if we're filtering we should stop on the first greater entry */
248 if (filter_vlan
&& vid
> filter_vlan
&&
249 !(flags
& BRIDGE_VLAN_INFO_RANGE_END
))
251 if ((flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) ||
258 static void print_vlan_port(FILE *fp
, int ifi_index
)
261 jsonw_pretty(jw_global
, 1);
262 jsonw_name(jw_global
,
263 ll_index_to_name(ifi_index
));
264 jsonw_start_array(jw_global
);
267 ll_index_to_name(ifi_index
));
271 static void start_json_vlan_flags_array(bool *vlan_flags
)
275 jsonw_name(jw_global
, "flags");
276 jsonw_start_array(jw_global
);
280 static void print_vlan_tunnel_info(FILE *fp
, struct rtattr
*tb
, int ifindex
)
282 bool jsonw_end_parray
= false;
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 print_vlan_port(fp
, ifindex
);
290 jsonw_end_parray
= 1;
293 for (i
= RTA_DATA(list
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
294 struct rtattr
*ttb
[IFLA_BRIDGE_VLAN_TUNNEL_MAX
+1];
296 __u16 tunnel_vid
= 0;
297 __u16 tunnel_flags
= 0;
300 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_TUNNEL_INFO
)
303 parse_rtattr(ttb
, IFLA_BRIDGE_VLAN_TUNNEL_MAX
,
304 RTA_DATA(i
), RTA_PAYLOAD(i
));
306 if (ttb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
])
308 rta_getattr_u32(ttb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
]);
312 if (ttb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
])
314 rta_getattr_u32(ttb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
]);
316 if (ttb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
])
318 rta_getattr_u32(ttb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
]);
320 if (!(tunnel_flags
& BRIDGE_VLAN_INFO_RANGE_END
)) {
321 last_vid_start
= tunnel_vid
;
322 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 print_vlan_port(fp
, ifindex
);
335 jsonw_end_parray
= 1;
339 jsonw_start_object(jw_global
);
340 jsonw_uint_field(jw_global
, "vlan",
343 fprintf(fp
, "\t %hu", last_vid_start
);
345 if (last_vid_start
!= tunnel_vid
) {
347 jsonw_uint_field(jw_global
, "vlanEnd",
350 fprintf(fp
, "-%hu", tunnel_vid
);
354 jsonw_uint_field(jw_global
, "tunid",
357 fprintf(fp
, "\t %hu", last_tunid_start
);
359 if (last_vid_start
!= tunnel_vid
) {
361 jsonw_uint_field(jw_global
, "tunidEnd",
364 fprintf(fp
, "-%hu", tunnel_id
);
368 jsonw_end_object(jw_global
);
373 if (jsonw_end_parray
) {
375 jsonw_end_array(jw_global
);
381 static int print_vlan_tunnel(const struct sockaddr_nl
*who
,
385 struct ifinfomsg
*ifm
= NLMSG_DATA(n
);
386 struct rtattr
*tb
[IFLA_MAX
+1];
387 int len
= n
->nlmsg_len
;
390 if (n
->nlmsg_type
!= RTM_NEWLINK
) {
391 fprintf(stderr
, "Not RTM_NEWLINK: %08x %08x %08x\n",
392 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
396 len
-= NLMSG_LENGTH(sizeof(*ifm
));
398 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
402 if (ifm
->ifi_family
!= AF_BRIDGE
)
405 if (filter_index
&& filter_index
!= ifm
->ifi_index
)
408 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifm
), len
);
410 /* if AF_SPEC isn't there, vlan table is not preset for this port */
411 if (!tb
[IFLA_AF_SPEC
]) {
412 if (!filter_vlan
&& !jw_global
)
413 fprintf(fp
, "%s\tNone\n",
414 ll_index_to_name(ifm
->ifi_index
));
418 print_vlan_tunnel_info(fp
, tb
[IFLA_AF_SPEC
], ifm
->ifi_index
);
424 static int print_vlan(const struct sockaddr_nl
*who
,
429 struct ifinfomsg
*ifm
= NLMSG_DATA(n
);
430 int len
= n
->nlmsg_len
;
431 struct rtattr
*tb
[IFLA_MAX
+1];
433 if (n
->nlmsg_type
!= RTM_NEWLINK
) {
434 fprintf(stderr
, "Not RTM_NEWLINK: %08x %08x %08x\n",
435 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
439 len
-= NLMSG_LENGTH(sizeof(*ifm
));
441 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
445 if (ifm
->ifi_family
!= AF_BRIDGE
)
448 if (filter_index
&& filter_index
!= ifm
->ifi_index
)
451 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifm
), len
);
453 /* if AF_SPEC isn't there, vlan table is not preset for this port */
454 if (!tb
[IFLA_AF_SPEC
]) {
455 if (!filter_vlan
&& !jw_global
)
456 fprintf(fp
, "%s\tNone\n",
457 ll_index_to_name(ifm
->ifi_index
));
461 print_vlan_info(fp
, tb
[IFLA_AF_SPEC
], ifm
->ifi_index
);
467 static void print_one_vlan_stats(FILE *fp
,
468 const struct bridge_vlan_xstats
*vstats
,
471 const char *ifname
= "";
473 if (filter_vlan
&& filter_vlan
!= vstats
->vid
)
475 /* skip pure port entries, they'll be dumped via the slave stats call */
476 if ((vstats
->flags
& BRIDGE_VLAN_INFO_MASTER
) &&
477 !(vstats
->flags
& BRIDGE_VLAN_INFO_BRENTRY
))
480 if (last_ifidx
!= ifindex
) {
481 ifname
= ll_index_to_name(ifindex
);
482 last_ifidx
= ifindex
;
484 fprintf(fp
, "%-16s %hu", ifname
, vstats
->vid
);
485 if (vstats
->flags
& BRIDGE_VLAN_INFO_PVID
)
486 fprintf(fp
, " PVID");
487 if (vstats
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
488 fprintf(fp
, " Egress Untagged");
490 fprintf(fp
, "%-16s RX: %llu bytes %llu packets\n",
491 "", vstats
->rx_bytes
, vstats
->rx_packets
);
492 fprintf(fp
, "%-16s TX: %llu bytes %llu packets\n",
493 "", vstats
->tx_bytes
, vstats
->tx_packets
);
496 static void print_vlan_stats_attr(FILE *fp
, struct rtattr
*attr
, int ifindex
)
498 struct rtattr
*brtb
[LINK_XSTATS_TYPE_MAX
+1];
499 struct rtattr
*i
, *list
;
502 parse_rtattr(brtb
, LINK_XSTATS_TYPE_MAX
, RTA_DATA(attr
),
504 if (!brtb
[LINK_XSTATS_TYPE_BRIDGE
])
507 list
= brtb
[LINK_XSTATS_TYPE_BRIDGE
];
508 rem
= RTA_PAYLOAD(list
);
509 for (i
= RTA_DATA(list
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
510 if (i
->rta_type
!= BRIDGE_XSTATS_VLAN
)
512 print_one_vlan_stats(fp
, RTA_DATA(i
), ifindex
);
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(fp
, tb
[IFLA_STATS_LINK_XSTATS
],
541 if (tb
[IFLA_STATS_LINK_XSTATS_SLAVE
])
542 print_vlan_stats_attr(fp
, 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
= if_nametoindex(filter_dev
);
571 if (filter_index
== 0) {
572 fprintf(stderr
, "Cannot find device \"%s\"\n",
579 if (rtnl_wilddump_req_filter(&rth
, PF_BRIDGE
, RTM_GETLINK
,
581 RTEXT_FILTER_BRVLAN_COMPRESSED
:
582 RTEXT_FILTER_BRVLAN
)) < 0) {
583 perror("Cannont send dump request");
587 jw_global
= jsonw_new(stdout
);
589 fprintf(stderr
, "Error allocation json object\n");
592 jsonw_start_object(jw_global
);
594 if (show_vlan_tunnel_info
)
595 printf("port\tvlan ids\ttunnel id\n");
597 printf("port\tvlan ids\n");
600 if (show_vlan_tunnel_info
)
601 ret
= rtnl_dump_filter(&rth
, print_vlan_tunnel
,
604 ret
= rtnl_dump_filter(&rth
, print_vlan
, stdout
);
607 fprintf(stderr
, "Dump ternminated\n");
613 filt_mask
= IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS
);
614 if (rtnl_wilddump_stats_req_filter(&rth
, AF_UNSPEC
,
617 perror("Cannont send dump request");
621 printf("%-16s vlan id\n", "port");
622 if (rtnl_dump_filter(&rth
, print_vlan_stats
, stdout
) < 0) {
623 fprintf(stderr
, "Dump terminated\n");
627 filt_mask
= IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS_SLAVE
);
628 if (rtnl_wilddump_stats_req_filter(&rth
, AF_UNSPEC
,
631 perror("Cannont send slave dump request");
635 if (rtnl_dump_filter(&rth
, print_vlan_stats
, stdout
) < 0) {
636 fprintf(stderr
, "Dump terminated\n");
642 jsonw_end_object(jw_global
);
643 jsonw_destroy(&jw_global
);
649 void print_vlan_info(FILE *fp
, struct rtattr
*tb
, int ifindex
)
651 struct rtattr
*i
, *list
= tb
;
652 int rem
= RTA_PAYLOAD(list
);
653 __u16 last_vid_start
= 0;
654 bool vlan_flags
= false;
655 bool jsonw_end_parray
= false;
658 print_vlan_port(fp
, ifindex
);
659 jsonw_end_parray
= true;
662 for (i
= RTA_DATA(list
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
663 struct bridge_vlan_info
*vinfo
;
666 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_INFO
)
671 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_END
))
672 last_vid_start
= vinfo
->vid
;
673 vcheck_ret
= filter_vlan_check(vinfo
->vid
, vinfo
->flags
);
674 if (vcheck_ret
== -1)
676 else if (vcheck_ret
== 0)
680 print_vlan_port(fp
, ifindex
);
681 jsonw_end_parray
= true;
684 jsonw_start_object(jw_global
);
685 jsonw_uint_field(jw_global
, "vlan",
687 if (vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
)
690 fprintf(fp
, "\t %hu", last_vid_start
);
692 if (last_vid_start
!= vinfo
->vid
) {
694 jsonw_uint_field(jw_global
, "vlanEnd",
697 fprintf(fp
, "-%hu", vinfo
->vid
);
699 if (vinfo
->flags
& BRIDGE_VLAN_INFO_PVID
) {
701 start_json_vlan_flags_array(&vlan_flags
);
702 jsonw_string(jw_global
, "PVID");
704 fprintf(fp
, " PVID");
707 if (vinfo
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
) {
709 start_json_vlan_flags_array(&vlan_flags
);
710 jsonw_string(jw_global
,
713 fprintf(fp
, " Egress Untagged");
716 if (jw_global
&& vlan_flags
) {
717 jsonw_end_array(jw_global
);
722 jsonw_end_object(jw_global
);
727 if (jsonw_end_parray
) {
729 jsonw_end_array(jw_global
);
736 int do_vlan(int argc
, char **argv
)
741 if (matches(*argv
, "add") == 0)
742 return vlan_modify(RTM_SETLINK
, argc
-1, argv
+1);
743 if (matches(*argv
, "delete") == 0)
744 return vlan_modify(RTM_DELLINK
, argc
-1, argv
+1);
745 if (matches(*argv
, "show") == 0 ||
746 matches(*argv
, "lst") == 0 ||
747 matches(*argv
, "list") == 0)
748 return vlan_show(argc
-1, argv
+1);
749 if (matches(*argv
, "tunnelshow") == 0) {
750 show_vlan_tunnel_info
= 1;
751 return vlan_show(argc
-1, argv
+1);
753 if (matches(*argv
, "help") == 0)
756 return vlan_show(0, NULL
);
759 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge vlan help\".\n", *argv
);