]>
git.proxmox.com Git - mirror_iproute2.git/blob - bridge/mdb.c
2 * Get mdb table with netlink
9 #include <sys/socket.h>
11 #include <netinet/in.h>
12 #include <linux/if_bridge.h>
13 #include <linux/if_ether.h>
15 #include <arpa/inet.h>
17 #include "libnetlink.h"
18 #include "br_common.h"
24 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct br_port_msg))))
27 static unsigned int filter_index
, filter_vlan
;
29 static void usage(void)
31 fprintf(stderr
, "Usage: bridge mdb { add | del } dev DEV port PORT grp GROUP [permanent | temp] [vid VID]\n");
32 fprintf(stderr
, " bridge mdb {show} [ dev DEV ] [ vid VID ]\n");
36 static bool is_temp_mcast_rtr(__u8 type
)
38 return type
== MDB_RTR_TYPE_TEMP_QUERY
|| type
== MDB_RTR_TYPE_TEMP
;
41 static void __print_router_port_stats(FILE *f
, struct rtattr
*pattr
)
43 struct rtattr
*tb
[MDBA_ROUTER_PATTR_MAX
+ 1];
47 parse_rtattr(tb
, MDBA_ROUTER_PATTR_MAX
, MDB_RTR_RTA(RTA_DATA(pattr
)),
48 RTA_PAYLOAD(pattr
) - RTA_ALIGN(sizeof(uint32_t)));
49 if (tb
[MDBA_ROUTER_PATTR_TIMER
]) {
51 rta_getattr_u32(tb
[MDBA_ROUTER_PATTR_TIMER
]));
52 fprintf(f
, " %4i.%.2i",
53 (int)tv
.tv_sec
, (int)tv
.tv_usec
/10000);
55 if (tb
[MDBA_ROUTER_PATTR_TYPE
]) {
56 type
= rta_getattr_u8(tb
[MDBA_ROUTER_PATTR_TYPE
]);
58 is_temp_mcast_rtr(type
) ? "temp" : "permanent");
62 static void br_print_router_ports(FILE *f
, struct rtattr
*attr
, __u32 brifidx
)
64 uint32_t *port_ifindex
;
69 fprintf(f
, "router ports on %s: ", ll_index_to_name(brifidx
));
71 rem
= RTA_PAYLOAD(attr
);
72 for (i
= RTA_DATA(attr
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
73 port_ifindex
= RTA_DATA(i
);
75 fprintf(f
, "router ports on %s: %s",
76 ll_index_to_name(brifidx
),
77 ll_index_to_name(*port_ifindex
));
78 __print_router_port_stats(f
, i
);
81 fprintf(f
, "%s ", ll_index_to_name(*port_ifindex
));
88 static void print_mdb_entry(FILE *f
, int ifindex
, struct br_mdb_entry
*e
,
89 struct nlmsghdr
*n
, struct rtattr
**tb
)
95 if (filter_vlan
&& e
->vid
!= filter_vlan
)
97 af
= e
->addr
.proto
== htons(ETH_P_IP
) ? AF_INET
: AF_INET6
;
98 src
= af
== AF_INET
? (const void *)&e
->addr
.u
.ip4
:
99 (const void *)&e
->addr
.u
.ip6
;
100 if (n
->nlmsg_type
== RTM_DELMDB
)
101 fprintf(f
, "Deleted ");
102 fprintf(f
, "dev %s port %s grp %s %s %s", ll_index_to_name(ifindex
),
103 ll_index_to_name(e
->ifindex
),
104 inet_ntop(af
, src
, abuf
, sizeof(abuf
)),
105 (e
->state
& MDB_PERMANENT
) ? "permanent" : "temp",
106 (e
->flags
& MDB_FLAGS_OFFLOAD
) ? "offload" : "");
108 fprintf(f
, " vid %hu", e
->vid
);
109 if (show_stats
&& tb
&& tb
[MDBA_MDB_EATTR_TIMER
]) {
112 __jiffies_to_tv(&tv
, rta_getattr_u32(tb
[MDBA_MDB_EATTR_TIMER
]));
113 fprintf(f
, "%4i.%.2i", (int)tv
.tv_sec
, (int)tv
.tv_usec
/10000);
118 static void br_print_mdb_entry(FILE *f
, int ifindex
, struct rtattr
*attr
,
121 struct rtattr
*etb
[MDBA_MDB_EATTR_MAX
+ 1];
122 struct br_mdb_entry
*e
;
126 rem
= RTA_PAYLOAD(attr
);
127 for (i
= RTA_DATA(attr
); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
)) {
129 parse_rtattr(etb
, MDBA_MDB_EATTR_MAX
, MDB_RTA(RTA_DATA(i
)),
130 RTA_PAYLOAD(i
) - RTA_ALIGN(sizeof(*e
)));
131 print_mdb_entry(f
, ifindex
, e
, n
, etb
);
135 int print_mdb(const struct sockaddr_nl
*who
, struct nlmsghdr
*n
, void *arg
)
138 struct br_port_msg
*r
= NLMSG_DATA(n
);
139 int len
= n
->nlmsg_len
;
140 struct rtattr
*tb
[MDBA_MAX
+1], *i
;
142 if (n
->nlmsg_type
!= RTM_GETMDB
&& n
->nlmsg_type
!= RTM_NEWMDB
&& n
->nlmsg_type
!= RTM_DELMDB
) {
143 fprintf(stderr
, "Not RTM_GETMDB, RTM_NEWMDB or RTM_DELMDB: %08x %08x %08x\n",
144 n
->nlmsg_len
, n
->nlmsg_type
, n
->nlmsg_flags
);
149 len
-= NLMSG_LENGTH(sizeof(*r
));
151 fprintf(stderr
, "BUG: wrong nlmsg len %d\n", len
);
155 if (filter_index
&& filter_index
!= r
->ifindex
)
158 parse_rtattr(tb
, MDBA_MAX
, MDBA_RTA(r
), n
->nlmsg_len
- NLMSG_LENGTH(sizeof(*r
)));
161 int rem
= RTA_PAYLOAD(tb
[MDBA_MDB
]);
163 for (i
= RTA_DATA(tb
[MDBA_MDB
]); RTA_OK(i
, rem
); i
= RTA_NEXT(i
, rem
))
164 br_print_mdb_entry(fp
, r
->ifindex
, i
, n
);
167 if (tb
[MDBA_ROUTER
]) {
168 if (n
->nlmsg_type
== RTM_GETMDB
) {
170 br_print_router_ports(fp
, tb
[MDBA_ROUTER
],
173 uint32_t *port_ifindex
;
175 i
= RTA_DATA(tb
[MDBA_ROUTER
]);
176 port_ifindex
= RTA_DATA(i
);
177 if (n
->nlmsg_type
== RTM_DELMDB
)
178 fprintf(fp
, "Deleted ");
179 fprintf(fp
, "router port dev %s master %s\n",
180 ll_index_to_name(*port_ifindex
),
181 ll_index_to_name(r
->ifindex
));
190 static int mdb_show(int argc
, char **argv
)
192 char *filter_dev
= NULL
;
195 if (strcmp(*argv
, "dev") == 0) {
198 duparg("dev", *argv
);
200 } else if (strcmp(*argv
, "vid") == 0) {
203 duparg("vid", *argv
);
204 filter_vlan
= atoi(*argv
);
210 filter_index
= if_nametoindex(filter_dev
);
211 if (filter_index
== 0) {
212 fprintf(stderr
, "Cannot find device \"%s\"\n",
218 if (rtnl_wilddump_request(&rth
, PF_BRIDGE
, RTM_GETMDB
) < 0) {
219 perror("Cannot send dump request");
223 if (rtnl_dump_filter(&rth
, print_mdb
, stdout
) < 0) {
224 fprintf(stderr
, "Dump terminated\n");
231 static int mdb_modify(int cmd
, int flags
, int argc
, char **argv
)
235 struct br_port_msg bpm
;
238 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct br_port_msg
)),
239 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
241 .bpm
.family
= PF_BRIDGE
,
243 struct br_mdb_entry entry
= {};
244 char *d
= NULL
, *p
= NULL
, *grp
= NULL
;
248 if (strcmp(*argv
, "dev") == 0) {
251 } else if (strcmp(*argv
, "grp") == 0) {
254 } else if (strcmp(*argv
, "port") == 0) {
257 } else if (strcmp(*argv
, "permanent") == 0) {
258 if (cmd
== RTM_NEWMDB
)
259 entry
.state
|= MDB_PERMANENT
;
260 } else if (strcmp(*argv
, "temp") == 0) {
262 } else if (strcmp(*argv
, "vid") == 0) {
266 if (matches(*argv
, "help") == 0)
272 if (d
== NULL
|| grp
== NULL
|| p
== NULL
) {
273 fprintf(stderr
, "Device, group address and port name are required arguments.\n");
277 req
.bpm
.ifindex
= ll_name_to_index(d
);
278 if (req
.bpm
.ifindex
== 0) {
279 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
283 entry
.ifindex
= ll_name_to_index(p
);
284 if (entry
.ifindex
== 0) {
285 fprintf(stderr
, "Cannot find device \"%s\"\n", p
);
289 if (!inet_pton(AF_INET
, grp
, &entry
.addr
.u
.ip4
)) {
290 if (!inet_pton(AF_INET6
, grp
, &entry
.addr
.u
.ip6
)) {
291 fprintf(stderr
, "Invalid address \"%s\"\n", grp
);
294 entry
.addr
.proto
= htons(ETH_P_IPV6
);
296 entry
.addr
.proto
= htons(ETH_P_IP
);
299 addattr_l(&req
.n
, sizeof(req
), MDBA_SET_ENTRY
, &entry
, sizeof(entry
));
301 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
307 int do_mdb(int argc
, char **argv
)
312 if (matches(*argv
, "add") == 0)
313 return mdb_modify(RTM_NEWMDB
, NLM_F_CREATE
|NLM_F_EXCL
, argc
-1, argv
+1);
314 if (matches(*argv
, "delete") == 0)
315 return mdb_modify(RTM_DELMDB
, 0, argc
-1, argv
+1);
317 if (matches(*argv
, "show") == 0 ||
318 matches(*argv
, "lst") == 0 ||
319 matches(*argv
, "list") == 0)
320 return mdb_show(argc
-1, argv
+1);
321 if (matches(*argv
, "help") == 0)
324 return mdb_show(0, NULL
);
326 fprintf(stderr
, "Command \"%s\" is unknown, try \"bridge mdb help\".\n", *argv
);