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 print_vlan_port(FILE *fp
, int ifi_index
)
257 print_string(PRINT_ANY
, NULL
, "%s",
258 ll_index_to_name(ifi_index
));
261 static void print_range(const char *name
, __u16 start
, __u16 id
)
265 snprintf(end
, sizeof(end
), "%sEnd", name
);
267 print_hu(PRINT_ANY
, name
, "\t %hu", start
);
269 print_hu(PRINT_ANY
, end
, "-%hu", id
);
273 static void print_vlan_tunnel_info(FILE *fp
, struct rtattr
*tb
, int ifindex
)
275 struct rtattr
*i
, *list
= tb
;
276 int rem
= RTA_PAYLOAD(list
);
277 __u16 last_vid_start
= 0;
278 __u32 last_tunid_start
= 0;
281 print_vlan_port(fp
, ifindex
);
283 open_json_array(PRINT_JSON
, "tunnel");
284 for (i
= RTA_DATA(list
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
285 struct rtattr
*ttb
[IFLA_BRIDGE_VLAN_TUNNEL_MAX
+1];
287 __u16 tunnel_vid
= 0;
288 __u16 tunnel_flags
= 0;
291 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_TUNNEL_INFO
)
294 parse_rtattr(ttb
, IFLA_BRIDGE_VLAN_TUNNEL_MAX
,
295 RTA_DATA(i
), RTA_PAYLOAD(i
));
297 if (ttb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
])
299 rta_getattr_u32(ttb
[IFLA_BRIDGE_VLAN_TUNNEL_VID
]);
303 if (ttb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
])
305 rta_getattr_u32(ttb
[IFLA_BRIDGE_VLAN_TUNNEL_ID
]);
307 if (ttb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
])
309 rta_getattr_u32(ttb
[IFLA_BRIDGE_VLAN_TUNNEL_FLAGS
]);
311 if (!(tunnel_flags
& BRIDGE_VLAN_INFO_RANGE_END
)) {
312 last_vid_start
= tunnel_vid
;
313 last_tunid_start
= tunnel_id
;
316 vcheck_ret
= filter_vlan_check(tunnel_vid
, tunnel_flags
);
317 if (vcheck_ret
== -1)
319 else if (vcheck_ret
== 0)
322 if (tunnel_flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
)
326 print_vlan_port(fp
, ifindex
);
328 open_json_object(NULL
);
329 print_range("vlan", last_vid_start
, tunnel_vid
);
330 print_range("tunid", last_tunid_start
, tunnel_id
);
333 if (!is_json_context())
337 close_json_array(PRINT_JSON
, NULL
);
340 static int print_vlan_tunnel(const struct sockaddr_nl
*who
,
344 struct ifinfomsg
*ifm
= NLMSG_DATA(n
);
345 struct rtattr
*tb
[IFLA_MAX
+1];
346 int len
= n
->nlmsg_len
;
349 if (n
->nlmsg_type
!= RTM_NEWLINK
) {
350 fprintf(stderr
, "Not RTM_NEWLINK: %08x %08x %08x\n",
351 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
355 len
-= NLMSG_LENGTH(sizeof(*ifm
));
357 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
361 if (ifm
->ifi_family
!= AF_BRIDGE
)
364 if (filter_index
&& filter_index
!= ifm
->ifi_index
)
367 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifm
), len
);
369 /* if AF_SPEC isn't there, vlan table is not preset for this port */
370 if (!tb
[IFLA_AF_SPEC
]) {
371 if (!filter_vlan
&& !is_json_context()) {
372 color_fprintf(fp
, COLOR_IFNAME
, "%s",
373 ll_index_to_name(ifm
->ifi_index
));
374 fprintf(fp
, "\tNone\n");
379 print_vlan_tunnel_info(fp
, tb
[IFLA_AF_SPEC
], ifm
->ifi_index
);
385 static int print_vlan(const struct sockaddr_nl
*who
,
390 struct ifinfomsg
*ifm
= NLMSG_DATA(n
);
391 int len
= n
->nlmsg_len
;
392 struct rtattr
*tb
[IFLA_MAX
+1];
394 if (n
->nlmsg_type
!= RTM_NEWLINK
) {
395 fprintf(stderr
, "Not RTM_NEWLINK: %08x %08x %08x\n",
396 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
400 len
-= NLMSG_LENGTH(sizeof(*ifm
));
402 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
406 if (ifm
->ifi_family
!= AF_BRIDGE
)
409 if (filter_index
&& filter_index
!= ifm
->ifi_index
)
412 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifm
), len
);
414 /* if AF_SPEC isn't there, vlan table is not preset for this port */
415 if (!tb
[IFLA_AF_SPEC
]) {
416 if (!filter_vlan
&& !is_json_context()) {
417 color_fprintf(fp
, COLOR_IFNAME
, "%s",
418 ll_index_to_name(ifm
->ifi_index
));
419 fprintf(fp
, "\tNone\n");
424 print_vlan_port(fp
, ifm
->ifi_index
);
425 print_vlan_info(fp
, tb
[IFLA_AF_SPEC
]);
431 static void print_vlan_flags(__u16 flags
)
433 if (flags
& BRIDGE_VLAN_INFO_PVID
)
434 print_null(PRINT_ANY
, "pvid", " %s", "PVID");
436 if (flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
437 print_null(PRINT_ANY
, "untagged", " %s", "untagged");
440 static void print_one_vlan_stats(const struct bridge_vlan_xstats
*vstats
)
442 open_json_object(NULL
);
443 print_hu(PRINT_ANY
, "vid", " %hu", vstats
->vid
);
445 print_vlan_flags(vstats
->flags
);
447 print_lluint(PRINT_ANY
, "rx_bytes",
450 print_lluint(PRINT_ANY
, "rx_packets", " %llu packets\n",
452 print_lluint(PRINT_ANY
, "tx_bytes",
455 print_lluint(PRINT_ANY
, "tx_packets", " %llu packets",
460 static void print_vlan_stats_attr(struct rtattr
*attr
, int ifindex
)
462 struct rtattr
*brtb
[LINK_XSTATS_TYPE_MAX
+1];
463 struct rtattr
*i
, *list
;
466 parse_rtattr(brtb
, LINK_XSTATS_TYPE_MAX
, RTA_DATA(attr
),
468 if (!brtb
[LINK_XSTATS_TYPE_BRIDGE
])
471 list
= brtb
[LINK_XSTATS_TYPE_BRIDGE
];
472 rem
= RTA_PAYLOAD(list
);
474 open_json_object(NULL
);
476 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
478 ll_index_to_name(ifindex
));
480 open_json_array(PRINT_JSON
, "xstats");
481 for (i
= RTA_DATA(list
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
482 const struct bridge_vlan_xstats
*vstats
= RTA_DATA(i
);
484 if (i
->rta_type
!= BRIDGE_XSTATS_VLAN
)
487 if (filter_vlan
&& filter_vlan
!= vstats
->vid
)
490 /* skip pure port entries, they'll be dumped via the slave stats call */
491 if ((vstats
->flags
& BRIDGE_VLAN_INFO_MASTER
) &&
492 !(vstats
->flags
& BRIDGE_VLAN_INFO_BRENTRY
))
495 print_one_vlan_stats(vstats
);
497 close_json_array(PRINT_ANY
, "\n");
502 static int print_vlan_stats(const struct sockaddr_nl
*who
,
506 struct if_stats_msg
*ifsm
= NLMSG_DATA(n
);
507 struct rtattr
*tb
[IFLA_STATS_MAX
+1];
508 int len
= n
->nlmsg_len
;
511 len
-= NLMSG_LENGTH(sizeof(*ifsm
));
513 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
517 if (filter_index
&& filter_index
!= ifsm
->ifindex
)
520 parse_rtattr(tb
, IFLA_STATS_MAX
, IFLA_STATS_RTA(ifsm
), len
);
522 /* We have to check if any of the two attrs are usable */
523 if (tb
[IFLA_STATS_LINK_XSTATS
])
524 print_vlan_stats_attr(tb
[IFLA_STATS_LINK_XSTATS
],
527 if (tb
[IFLA_STATS_LINK_XSTATS_SLAVE
])
528 print_vlan_stats_attr(tb
[IFLA_STATS_LINK_XSTATS_SLAVE
],
535 static int vlan_show(int argc
, char **argv
)
537 char *filter_dev
= NULL
;
541 if (strcmp(*argv
, "dev") == 0) {
544 duparg("dev", *argv
);
546 } else if (strcmp(*argv
, "vid") == 0) {
549 duparg("vid", *argv
);
550 filter_vlan
= atoi(*argv
);
556 filter_index
= ll_name_to_index(filter_dev
);
557 if (filter_index
== 0) {
558 fprintf(stderr
, "Cannot find device \"%s\"\n",
567 if (rtnl_wilddump_req_filter(&rth
, PF_BRIDGE
, RTM_GETLINK
,
569 RTEXT_FILTER_BRVLAN_COMPRESSED
:
570 RTEXT_FILTER_BRVLAN
)) < 0) {
571 perror("Cannont send dump request");
575 if (!is_json_context()) {
576 if (show_vlan_tunnel_info
)
577 printf("port\tvlan ids\ttunnel id\n");
579 printf("port\tvlan ids\n");
582 if (show_vlan_tunnel_info
)
583 ret
= rtnl_dump_filter(&rth
, print_vlan_tunnel
,
586 ret
= rtnl_dump_filter(&rth
, print_vlan
, stdout
);
588 fprintf(stderr
, "Dump ternminated\n");
594 filt_mask
= IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS
);
595 if (rtnl_wilddump_stats_req_filter(&rth
, AF_UNSPEC
,
598 perror("Cannont send dump request");
602 if (!is_json_context())
603 printf("%-16s vlan id\n", "port");
605 if (rtnl_dump_filter(&rth
, print_vlan_stats
, stdout
) < 0) {
606 fprintf(stderr
, "Dump terminated\n");
610 filt_mask
= IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_XSTATS_SLAVE
);
611 if (rtnl_wilddump_stats_req_filter(&rth
, AF_UNSPEC
,
614 perror("Cannont send slave dump request");
618 if (rtnl_dump_filter(&rth
, print_vlan_stats
, stdout
) < 0) {
619 fprintf(stderr
, "Dump terminated\n");
629 void print_vlan_info(FILE *fp
, struct rtattr
*tb
)
631 struct rtattr
*i
, *list
= tb
;
632 int rem
= RTA_PAYLOAD(list
);
633 __u16 last_vid_start
= 0;
635 if (!is_json_context())
636 fprintf(fp
, "%s", _SL_
);
638 open_json_array(PRINT_JSON
, "vlan");
640 for (i
= RTA_DATA(list
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
641 struct bridge_vlan_info
*vinfo
;
644 if (i
->rta_type
!= IFLA_BRIDGE_VLAN_INFO
)
649 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_END
))
650 last_vid_start
= vinfo
->vid
;
651 vcheck_ret
= filter_vlan_check(vinfo
->vid
, vinfo
->flags
);
652 if (vcheck_ret
== -1)
654 else if (vcheck_ret
== 0)
657 open_json_object(NULL
);
658 print_range("vlan", last_vid_start
, vinfo
->vid
);
660 print_vlan_flags(vinfo
->flags
);
664 close_json_array(PRINT_ANY
, "\n");
667 int do_vlan(int argc
, char **argv
)
672 if (matches(*argv
, "add") == 0)
673 return vlan_modify(RTM_SETLINK
, argc
-1, argv
+1);
674 if (matches(*argv
, "delete") == 0)
675 return vlan_modify(RTM_DELLINK
, argc
-1, argv
+1);
676 if (matches(*argv
, "show") == 0 ||
677 matches(*argv
, "lst") == 0 ||
678 matches(*argv
, "list") == 0)
679 return vlan_show(argc
-1, argv
+1);
680 if (matches(*argv
, "tunnelshow") == 0) {
681 show_vlan_tunnel_info
= 1;
682 return vlan_show(argc
-1, argv
+1);
684 if (matches(*argv
, "help") == 0)
687 return vlan_show(0, NULL
);
690 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge vlan help\".\n", *argv
);